Results 1 to 6 of 6
  1. #1
    Java Mark is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Sortable, Resizable Collection of Objects with Different Classes

    Quote Originally Posted by Java Mark View Post
    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.

    I know that this is not directly possible in Java with an Array or an ArrayList.
    Who told you that?

    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.

  3. #3
    Java Mark is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Sortable, Resizable Collection of Objects with Different Classes

    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."

    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.

    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.
    Last edited by Java Mark; 11-20-2011 at 11:45 PM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default 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.

  5. #5
    Java Mark is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Sortable, Resizable Collection of Objects with Different Classes

    Quote Originally Posted by Junky View Post
    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default 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.

Similar Threads

  1. Serialization of Collection classes
    By me.anchit in forum Advanced Java
    Replies: 6
    Last Post: 09-23-2011, 12:57 PM
  2. Diff on Collection classes
    By mopurilakshmikarreddy in forum New To Java
    Replies: 1
    Last Post: 06-08-2011, 09:31 AM
  3. Best way to handle a collection of objects?
    By mattlindsay in forum New To Java
    Replies: 4
    Last Post: 04-03-2011, 04:07 PM
  4. Counting vowles in a collection of String objects
    By sunde887 in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 07:22 AM
  5. iterating through a collection of objects
    By Scotty Boy in forum New To Java
    Replies: 0
    Last Post: 04-10-2008, 01:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •