DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2004

    ArrayList of Objects

    i want to have an array list of objects (QA2s which is my own object). i've declared them as follows:

            levelTwo.add(Level2, new QA2(qData.getQuestion(), qData.getPossible(0),
            qData.getPossible(1), qData.getPossible(2), qData.getPossible(3), qData.getAnswer()));
    levelTwo is the array list
    aData is a class that reads data in from a database.

    now with these added, how would i run a method in the QA2 object given it's possition in the array?

  2. #2
    Join Date
    Feb 2004
    Ok, I assume that Level2 is an int representing the index you're adding the object to in the ArrayList.

    ArrayList only stores things as type Object, hence allowing it to store anything you throw at it without caring about type. This means that when you get them back out of it you have to cast the Object back into its original type for it to be useful.

    To get the object you need to know the index it is stored at, and then you use the get method. Using your Level2 variable above, it would look something like this...

    QA2 obFromArrList = (QA2)level2.get(Level2);
    (The QA2 in brackets on the right of the equals is to cast the Object from the ArrayList back into a QA2 object)

  3. #3
    Join Date
    Nov 2004
    If you want to recognize an object (wherever it may
    be stored) you have two options, identify it by
    its address or by its method:
    boolean equals(Object ob);
    If you haven't defined any equals method for your
    class then the default equals() method is used, -
    the Object class' equals method, and this will be an
    object address comparison. That is not practical cause
    you may have the 'same' object defined twice (exactly the same content but w. different addresses).
    In that case you would want the equals() method return
    the value true.

    So in short, you define the equals method for your
    class like (here is a phoneBook typeof example):

    public boolean equals (Object ob) {
      if (!(ob instanceof MyClass)) return false;
      MyClass anObj=(MyClass)ob;
      return anObj.getName().equals(this.getName()) &&
    It is also recommended that you implement the method:
    public int hashCode();
    for a class that also has the equals()method defined.
    The default here is to use the object address for making
    the hashCode, and that is no good (if equals() returns
    true for two object then their hashCode should also
    be identical). But two objects w. the same
    hashCode doeas not have to be equal()

    Anyway, if you define the equals() method for your QA2
    class you could do like this:
    int ix=aList.indexOf(anObject);
    as this method uses the listelements equals() method.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center