Results 1 to 5 of 5
  1. #1
    xtrmi is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default ArrayList with different objects? Help

    Been looking around for the proper way to build an ArrayList that holds two different objects. Is this possible?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's quite easily possible, and one way is to create a non-generic ArrayList (an ArrayList without the <> part). Having said that, I don't recommend that you do this except in unusual circumstances. What type of two different objects precisely will this list be holding?

  3. #3
    xtrmi is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    It will hold to different objects that I have created, Router and Computer.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Do they both use the same base class? If so, use that base class as the generic type. Otherwise, although it kind of defeats the purpose, use Object. In either case, you will need to cast the retrieved objects to make "full use" of them once retrieved.

  5. #5
    xtrmi is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  2. Two Objects
    By losintikfos in forum New To Java
    Replies: 3
    Last Post: 11-14-2008, 08:04 PM
  3. 1 to 1 Objects
    By this.that in forum New To Java
    Replies: 4
    Last Post: 08-07-2008, 11:09 PM
  4. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 10:32 PM

Posting Permissions

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