Results 1 to 6 of 6
- 11-20-2011, 10:23 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Sortable, Resizable Collection of Objects with Different Classes
Looking for proven, common workarounds to Java's Array and ArrayList limitations.
I would like to be able to create a resizable collection with array-like characteristics that can hold references to objects of different classes.
Some of the array-like characteristics I would like:
- Append new elements with a method comparable to push() or add()
- Iterate through the collection using an index value and a for statement
- Access public methods of elements in the collection by refering to their index value in the collection
I know that this is not directly possible in Java with an Array or an ArrayList.
The closest seems to be ArrayList, which provides everything but the ability to access the public methods of elements in the ArrayList (because everything in an ArrayList gets typed as an Object).
I tried creating a simple custom class with two ArrayLists, one for the objects themselves and one for the types, which would be synchronized in order to return correctly-typed objects. However, this is awkward and doesn't work completely (inefficient additional code required to use the returned references).
A slightly better approach I'm thinking of would use a bunch of interfaces and would check to see if ArrayList elements implement certain interfaces in order to call methods. However, I'm not sure this will work, and it would significantly alter my approach (lots and lots of interfaces involved - which seems to move the member functions out of their classes - looks like a mess to me).
Currently, I would like to use this desired collection as a master list of all objects in a simulation, for which new classes may be added during development and future versions.
- 11-20-2011, 10:49 PM #2
Re: Sortable, Resizable Collection of Objects with Different Classes
ArrayList is qwhat you are looking for. However it depends upon what you mean by different objects. If you want to store a Dog, a Chair and a Telephone number then the question is why do you need to store disparate objects. If you want to store a Square, a Circle, a Triangle all which extend Shape then that is fine.
Who told you that?I know that this is not directly possible in Java with an Array or an ArrayList.
Then you need to explore Generics.The closest seems to be ArrayList, which provides everything but the ability to access the public methods of elements in the ArrayList (because everything in an ArrayList gets typed as an Object).
- 11-20-2011, 11:43 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: Sortable, Resizable Collection of Objects with Different Classes
"Currently, I would like to use this desired collection as a master list of all objects in a simulation, for which new classes may be added during development and future versions."If you want to store a Dog, a Chair and a Telephone number then the question is why do you need to store disparate objects. If you want to store a Square, a Circle, a Triangle all which extend Shape then that is fine.
Objects accessed from an ArrayList have the type Object. It does not matter what they were when you created them and passed them to the Array via add(). If I pass a reference to an instance of an Orange class, I will get an Object type when I try to access that reference in the ArrayList. Same thing with a reference to an instance of Apple class. So, trying to access references to objects of different types/classes in an ArrayList leaves one unable to tell Apples from Oranges.Who told you that?I know that this is not directly possible in Java with an Array or an ArrayList.
I have, and it looks like Generics allow you to establish a particular class or type for elements of an ArrayList, but only one type - so this does not overcome the limitation on storing references to objects of different classes in an ArrayList - unless I am missing some other features of Generics or some other way of using them. (Which I probably am...since I'm very new to Java.)Then you need to explore Generics.The closest seems to be ArrayList, which provides everything but the ability to access the public methods of elements in the ArrayList (because everything in an ArrayList gets typed as an Object).
I figured there was already a well-known workaround for this that I haven't been able to find via Google.
In ActionScript 3.0 (by no means a perfect language, or as powerful as Java), this is a piece of cake.Last edited by Java Mark; 11-20-2011 at 11:45 PM.
- 11-20-2011, 11:54 PM #4
Re: Sortable, Resizable Collection of Objects with Different Classes
If you want to keep all the objects your program creates, regardless of their type, in one Collection then this is a really bad idea and you should rethink your design.
- 11-21-2011, 01:27 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: Sortable, Resizable Collection of Objects with Different Classes
- 11-21-2011, 01:42 AM #6
Similar Threads
-
Serialization of Collection classes
By me.anchit in forum Advanced JavaReplies: 6Last Post: 09-23-2011, 12:57 PM -
Diff on Collection classes
By mopurilakshmikarreddy in forum New To JavaReplies: 1Last Post: 06-08-2011, 09:31 AM -
Best way to handle a collection of objects?
By mattlindsay in forum New To JavaReplies: 4Last Post: 04-03-2011, 04:07 PM -
Counting vowles in a collection of String objects
By sunde887 in forum New To JavaReplies: 4Last Post: 01-18-2011, 07:22 AM -
iterating through a collection of objects
By Scotty Boy in forum New To JavaReplies: 0Last Post: 04-10-2008, 01:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks