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.
Re: Sortable, Resizable Collection of Objects with Different Classes
Quote:
Originally Posted by
Java Mark
I would like to be able to create a resizable collection with array-like characteristics that can hold references to objects of 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.
Quote:
I know that this is not directly possible in Java with an Array or an ArrayList.
Who told you that?
Quote:
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).
Then you need to explore Generics.
Re: Sortable, Resizable Collection of Objects with Different Classes
Quote:
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.
"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."
Quote:
Quote:
I know that this is not directly possible in Java with an Array or an ArrayList.
Who told you that?
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.
Quote:
Quote:
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).
Then you need to explore Generics.
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.)
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.
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.
Re: Sortable, Resizable Collection of Objects with Different Classes
Quote:
Originally Posted by
Junky
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.
I don't want to keep all of the objects my program creates, regardless of their type, in one Collection.
I want to keep references to all of the objects in a specific subset of my program, regardless of their type, in one Collection.
Thanks for your input.
Re: Sortable, Resizable Collection of Objects with Different Classes
That is still a bad idea. The whole point of using data structures is to keep together like things. Keeping together unlike things is just wrong.