how can i put multiple classtypes in 1 array/arraylist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ciary
    Recognized Expert New Member
    • Apr 2009
    • 247

    how can i put multiple classtypes in 1 array/arraylist

    hi,

    i have a problem i can't figure out. i'm trying to create a tree structure in a class. to do that i have a class "Knod" and a class "Leaf". the class "Tree" is an ArrayList of arrays. and these arrays contain a boolean, a Knod or Leaf-class and 2 ints (defining the next Knod or Leaf)

    Code:
    public class Tree{
        private int init;
        private ArrayList<Object[]> boom;
    
        public Tree(){
            this.init = 0;
            this.boom = new ArrayList<Object[]>();
        }
    
        public void newLeaf(double[] waarden){
            Object[] arr = new Object[2];
            
                                    //false = leaf
            arr[0] = new Boolean(false);            //true = Knod
            arr[1] = new Leaf(waarden);
    
            this.boom.add(arr);
        }
    
        public void newKnod(double[] waarden, int trueVal, int falseVal){
            Object[] arr = new Object[4];
            
                                    //false = leaf
            arr[0] = new Boolean(true);            //true = Knod
            arr[1] = new Knod(waarden);
            arr[2] = new Integer(trueVal);
            arr[3] = new Integer(falseVal);
    
            this.boom.add(arr);
        }
    }
    as you can see i use an array of objects to make this work.
    now i want to get the value of the Boolean. usually you would do
    Code:
    bool.booleanValue()
    or in my case
    Code:
    this.boom.get(id)[0].booleanValue()
    but since i made an array of object's he doesn't recognize this function.

    so the actual question is, how can i put multiple classtypes in 1 array and still use all the functions?

    tnx
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Can't you use vectors or any other collection frame works?

    Regards
    Dheeraj Joshi

    Comment

    • Ciary
      Recognized Expert New Member
      • Apr 2009
      • 247

      #3
      please explain that a bit further. as far as i know, a vector and arraylist are the same but from a different language (but i'm probably wrong) Enlighten me

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        To come back to the original question, you'll have to cast the Objects. So in your example, that should be something like
        Code:
        ((Boolean[]) this.boom.get(id))[0].booleanValue()
        (Untested, but should work.)

        Greetings,
        Nepomuk

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Ciary,

          The difference between a Vector and an ArrayList is that the Vector class is synchronized. If you're writing a single thread program, you won't notice any difference between the two.

          Hopefully that helps.

          Luck!

          Comment

          • Ciary
            Recognized Expert New Member
            • Apr 2009
            • 247

            #6
            hi,

            i already solved the problem my own way already. i just made 4 arraylists instead of an arraylist of arrays. that way i can change the type of each arraylist separately.

            @Nepomuk: yup, that works. and it's probably better then my method. but since i'm just testing and efficiency isn't an issue i'll only change it if i get stuck.

            @Oralloy: i am writing a single thread program so there shouldn't be a difference. but tnx anways. Honestly, i know the name 'vector' from C++ and physics. i had no idea what meaning it had in Java.

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              Ciary,

              Synchronization requires CPU time. Use it when appropriate, but if it is not needed, then it's waste. But you probably already know that.

              What's wrong with using an ArrayList<MyCon tainer>, where MyContainer is a service class which has all four attributes. Then you access them using a relatively simple construct:
              Code:
              ArrayList<MyContainer> myArrayList = new ArrayList<MyContainer>();
              
              . . .
              
              myArrayList.get(index).attributeN;
              Luck!

              Comment

              Working...