Jump to content

  • Log In with Google      Sign In   
  • Create Account

Help with ArrayList Java


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Manhattanisgr8   Members   -  Reputation: 487

Like
0Likes
Like

Posted 15 August 2014 - 05:52 PM

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


Sponsor:

#2 megadan   GDNet+   -  Reputation: 586

Like
3Likes
Like

Posted 15 August 2014 - 10:50 PM

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.



#3 Manhattanisgr8   Members   -  Reputation: 487

Like
0Likes
Like

Posted 16 August 2014 - 03:23 AM

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


#4 patrrr   Members   -  Reputation: 1003

Like
1Likes
Like

Posted 16 August 2014 - 03:51 AM

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.


Edited by patrrr, 16 August 2014 - 03:53 AM.


#5 Shippou   Members   -  Reputation: 1522

Like
1Likes
Like

Posted 16 August 2014 - 05:38 AM

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 .


Edited by Shippou, 16 August 2014 - 12:39 PM.

 Reactions To Technologies:
1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works.
2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it.
3. Anything invented after you're thirty-five is against the natural order of things.

- Douglas Adams 2002


 


#6 rip-off   Moderators   -  Reputation: 8210

Like
4Likes
Like

Posted 16 August 2014 - 08:03 AM

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.



#7 patrrr   Members   -  Reputation: 1003

Like
1Likes
Like

Posted 16 August 2014 - 11:50 AM

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


Edited by patrrr, 16 August 2014 - 11:50 AM.


#8 Manhattanisgr8   Members   -  Reputation: 487

Like
0Likes
Like

Posted 16 August 2014 - 01:02 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS