ArrayList of Objects


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: ArrayList of Objects

  1. #1
    Join Date
    Oct 2004
    Posts
    11

    ArrayList of Objects

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

    Code:
            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
    Posts
    541
    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...

    Code:
    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
    Location
    Norway
    Posts
    1,560
    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:
    Code:
    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):

    Code:
    public boolean equals (Object ob) {
      if (!(ob instanceof MyClass)) return false;
      MyClass anObj=(MyClass)ob;
      return anObj.getName().equals(this.getName()) &&
                 anObj.getAddress().equals(this.getAddress());
    }
    It is also recommended that you implement the method:
    Code:
    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:
    Code:
    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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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