7 Replies Latest reply: Jul 24, 2007 3:42 PM by 807600 RSS

    which collection should i use to hold multiple diffrent objects

    807600
      Ok as the title states I have a program which uses multiple different objects which i want to put into some type of container such as a list. However I only used lists, and arraylsits before. I don't know if a list can handle it and i was always told arraylists can only hold 1 type of object. So is there a collection type that can hold multiple different types of object?. Also the closer it handles to an array list the better because I'm already fairly comfortable with those.

      I also have 1 more question. Say i have a class dog and a few different classes that extend it such as lab, poodle, ect. If i have a function that has the dog object as a parameter can i put a poodle or lab object in its place??

      thanks in advance for any help.
        • 1. Re: which collection should i use to hold multiple diffrent objects
          807600
          All collections/lists (linkedlist, arraylist, vector etc)
          hold Objects. Everything is a subclass of Object (except primitives).

          You can put anything you want into any collection/list.
          Of course this limits your use of Generics...
          • 2. Re: which collection should i use to hold multiple diffrent objects
            807600
            Ok as the title states I have a program which uses
            multiple different objects which i want to put into
            some type of container such as a list. However I only
            used lists, and arraylsits before. I don't know if a
            list can handle it and i was always told arraylists
            can only hold 1 type of object. So is there a
            collection type that can hold multiple different
            types of object?. Also the closer it handles to an
            array list the better because I'm already fairly
            comfortable with those.
            Can you explain more on your problem, usually it is not good to put multiple types in the same collection. What are you trying to achieve from this?
            I also have 1 more question. Say i have a class dog
            and a few different classes that extend it such as
            lab, poodle, ect. If i have a function that has the
            dog object as a parameter can i put a poodle or lab
            object in its place??

            thanks in advance for any help.
            Try it and see :-)
            • 3. Re: which collection should i use to hold multiple diffrent objects
              807600
              Can you explain more on your problem, usually it is not good to put multiple
              types in the same collection. What are you trying to achieve from this?
              sure I'm attempting a game very similar to a trading card game. I can make a base card object with info such as name, attack, defence, ect. However where I can't see using the same object is because each card has its own specific abilities. So i was thinking the best way is to make the base card object then extend it for every card. I relize this is possibly the worst way to do it but its the only way i can see how.


              Yes I'm not very experienced (just over a year of java) and i know this is fairly difficult and big project and i expect it to take quite a long time but i hope with your guys help and any tutorials i find around i can do this. Plus good old trial and error
              • 4. Re: which collection should i use to hold multiple diffrent objects
                807600
                Can you explain more on your problem, usually it is
                not good to put multiple
                types in the same collection. What are you trying
                to achieve from this?

                sure I'm attempting a game very similar to a trading
                card game. I can make a base card object with info
                such as name, attack, defence, ect. However where I
                can't see using the same object is because each card
                has its own specific abilities. So i was thinking the
                best way is to make the base card object then extend
                it for every card. I relize this is possibly the
                worst way to do it but its the only way i can see
                how.
                Not at all, that's actually a very good way to design your classes. If the base card is an interface or abstract class, then you can call it's methods and they'll use the implementations of the concrete classes. If you can design it so that the concrete classes can all use the same methods, that's the way to go.
                Yes I'm not very experienced (just over a year of
                java) and i know this is fairly difficult and big
                project and i expect it to take quite a long time but
                i hope with your guys help and any tutorials i find
                around i can do this. Plus good old trial and error
                If you need a tutorial on a specific subject, just google for "java <subject> tutorial" and you'll get plenty of help.
                • 5. Re: which collection should i use to hold multiple diffrent objects
                  807600
                  sure I'm attempting a game very similar to a trading
                  card game. I can make a base card object with info
                  such as name, attack, defence, ect. However where I
                  can't see using the same object is because each card
                  has its own specific abilities. So i was thinking the
                  best way is to make the base card object then extend
                  it for every card. I relize this is possibly the
                  worst way to do it but its the only way i can see
                  how.
                  If all the objects you want to put in your container inherit from some base class, let's call it Card, then make a container of Card objects. You could go with a List, ArrayList, Vector... whatever you need/like. For example, you could use
                  Vector<Card> myDeck = new Vector<Card>()
                  As previous poster mentioned, it would probably be wise to make Card an abstract class or an interface. Also, and hopefully you won't need to do this, but if some children of Card have specific members absent from the base class Card, then you could get at these through introspection.
                  • 6. Re: which collection should i use to hold multiple diffrent objects
                    807600
                    Can you explain more on your problem, usually it is
                    not good to put multiple
                    types in the same collection. What are you trying
                    to achieve from this?

                    sure I'm attempting a game very similar to a trading
                    card game. I can make a base card object with info
                    such as name, attack, defence, ect. However where I
                    can't see using the same object is because each card
                    has its own specific abilities. So i was thinking the
                    best way is to make the base card object then extend
                    it for every card. I relize this is possibly the
                    worst way to do it but its the only way i can see
                    how.
                    Yes this is okay, it is good in fact to do this, what I meant by multiple types was say a person and a car, these have nothing in common, so usually grouping them is bad.

                    unless all the Objects which (can still be quite different in nature) provide some service which they inherit from the same super type. This is binding them as a type, even it is just one method they inherit from an interface thats provides one service which is common between both types.

                    Here is a small example of when even one method or service (inherited from the same interface) is still enough to bind totally different Objects to one type via an interface.

                    Car and Person might not be the best example but I am sure you get the picture.
                    import java.util.ArrayList;
                    import java.util.List;
                    
                    
                    public class Example {
                    
                         public static void main(String[] args) {
                              
                              List<Service> list = new ArrayList<Service>();
                              list.add(new Person());
                              list.add(new Car());
                              
                              for (int i = 0; i < list.size(); i++) {
                                   list.get(i).provideService();
                              }
                         }
                    }
                    
                    interface Service {
                         
                         void provideService();
                    }
                    
                    class Person implements Service {
                    
                         public void provideService() {
                              System.out.println("I am a Person");
                         }
                         
                         //Lots of Person specific code below
                         //Which has nothing to do with Car
                         //...
                    }
                    
                    class Car implements Service {
                    
                         public void provideService() {
                              System.out.println("I am a car");
                         }
                         
                         //Lots of Car specific code below
                         //Which has nothing to do with Person
                         //...
                    }
                    • 7. Re: which collection should i use to hold multiple diffrent objects
                      807600
                      Wow thanks guys for all the help. So as of right now i need to go learn interfaces and abstract classes. As its set up right now all the cards have everything the same except there ability methods which i think i can just call abil1, abil2, and abil3 then just put the specialized code in. or maybe this is where that introspection idea comes in I'm not sure yet

                      Thanks for all the help so far everyone.