Help with ArrayList Java

Started by
6 comments, last by Manhattanisgr8 9 years, 8 months ago

I am currently working on a text based game using swing. In it, I have the user create a game world (essentially just a new folder name and then some serialized files in it). I a trying to make a way to import what data they want from one world to another. My problems is, instead of making a new popup for each object, I want to create just one and pass a string to it in order to get what serialized object for the popup to get. I have tried to turn the string into a class name via Class className = Class.forName("com.package.class") method, however, when I try to input that into ArrayList<className> arrayList, it tells me className cannot be resolved to a type. Is there anyway i can achieve this? Or am I going to have to make a popup for each object I want to have imported?

Cpl Alt, Travis A

USMC

Advertisement

Generics must be fully specified at compile time rather than run time. So you can't use a variable as the generic parameter. Depending on how you want to use it, you could do something like ArrayList<Class<?>> arrayList. This allows any class type to be in the list. Then you can fill it with className's of different actual types.

Thank you megadan, I now have it reading the ArrayList using Class<?>. Now my problem is adding elements to an ArrayList<Class> from ArrayList<Class<?>>.

Example:


ArrayList<Moves> moves;
ArrayList<Class<?>> importedMoves;

for (int x = 0; x < importedMoves.size(); x++){
moves.add(importedMoves.get(x));

Is there a way around this?

Cpl Alt, Travis A

USMC

Thank you megadan, I now have it reading the ArrayList using Class<?>. Now my problem is adding elements to an ArrayList<Class> from ArrayList<Class<?>>.

Example:


ArrayList<Moves> moves;
ArrayList<Class<?>> importedMoves;

for (int x = 0; x < importedMoves.size(); x++){
moves.add(importedMoves.get(x));

Is there a way around this?

Just remove the generics all together, it seems like you don't want type checking. This can cause problems in the future though (you'll need to type cast), and it'll also make the code harder to understand.

This code should theoretically work, however I haven't tested it ( getting ready for work as I type this message ) .


ArrayList <Object> Something = new ArrayList <Object>() ; 

"Object" is a generic that should be able to hold any kind of object .

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

It sounds like you want to take the class name from a file and instantiate objects of that class type. You can do the following:

Class<?> type = Class.forName("net.gamedev.SomeMoveClass");
Move move = (Move)type.newInstance();
moves.add(move);

If you need to pass arguments, you can use the getConstructor() method and attempt to call newInstance(/* ... */) on that.

You may want to do some sanity checking that the class does implement or extend the Move interface / superclass.

That said, this smells like possibly a more complex solution than you actually need. I'd have to understand the surrounding concepts a bit more to say for sure, but I'd guess there may be other, easier ways to do this.

This code should theoretically work, however I haven't tested it ( getting ready for work as I type this message ) .


ArrayList <Object> Something = new ArrayList <Object>() ; 

"Object" is a generic that should be able to hold any kind of Object .

Which is what ArrayList without the generics will result in, afaik smile.png

Thanks guys. The combination of using ArrayList<Object> and then casting the Object to a specific class type worked. You guys saved me several hours of coding. I can't thank you guys enough.

Cpl Alt, Travis A

USMC

This topic is closed to new replies.

Advertisement