Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with ArrayList Java

Started by Manhattanisgr8 Aug 15, 2014 at 11:52 PM 6 replies 4.2k views
Original Post
Manhattanisgr8
Manhattanisgr8

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 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
megadan
megadan

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> arrayList. This allows any class type to be in the list. Then you can fill it with className's of different actual types.

Manhattanisgr8
Manhattanisgr8

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

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
patrrr
patrrr

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

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.

RLS0812
RLS0812

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
rip-off
rip-off

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.

patrrr
patrrr

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

Manhattanisgr8
Manhattanisgr8

Thanks guys. The combination of using ArrayList 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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.