Is there a way to use an ArrayList which hold objects but not specific objects. Something like anonymous objects. For instance you dont know which objects its gonna be you just want that arraylist to hold objects
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Is there a way to use an ArrayList which hold objects but not specific objects. Something like anonymous objects. For instance you dont know which objects its gonna be you just want that arraylist to hold objects
Yes, you can create an array list of type Object
ArrayList<Object> objects = new ArrayList<Object>();
jps (August 10th, 2013)
This sounds like a bad idea. What purpose would you have for a List that contains Chair, Spoon, Dog etc? On the other hand if the goal was to have a List of randomly generated Fiat, Ford, Audi, BMW etc objects then perhaps those classes should all extend a super class and you make the type of your List that super class:
;ArrayList<CarMake> cars = new ArrayList<CarMake>();
Improving the world one idiot at a time!
jps (August 11th, 2013)