-
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?
-
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)
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks