Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


ArrayList and contains


Hey everyone! alright, i've had this question for a while. Say i have the following code  ArrayList<Aoeu> list = new ArrayList<Aoeu>(); list.add(new Aoeu(5)); list.add(new Aoeu(3)); list.add(new Aoeu(1));   System.out.println(list.contains(new Aoeu(1));   class Aoeu {     int a;          Aoeu(int a) { this.a = a;}       public boolean equals(Aoeu other) {         return this.a == other.a;     } }   it prints false!! even though i know it contains an Aoeu with value 1.  so my guess is that the problem lies in the equals method, so i rewrote it as:  public boolean equals(Object obj) {     Aoeu other = (Aoeu) obj; // i probably should do an instanceof check, but its just a test...     return this.a == other.a; }   And then it worked. I'm fine with it like this, but i'd like to know the reason why. I understand that the contains method works off of the equals method, but didn't i have an equals method in the first example? It's not overriding the one in Object, but since it has the same name, i thought maybe it would work.  Could someone explain the flaw in my logic? Thanks!

Well, it's as you said, you didn't override the equals method before. Overloaded methods are independent methods, which just happen to have the same name. ArrayList.contains calls Object.equals(Object). It does not know your custom equals method.  -Puce

ooh ok that makes sense. I went and looked at the API, and the heading for contains is: public boolean contains(Object o), so it would check for Objects (not random other classes).  thanks!

plutoisaplanet wrote:  // i probably should do an instanceof check, but its just a test...  But if you're interessted in how to write a good equals method, read chapter 3 of "Effective Java Programming Language Guide": http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf http://java.sun.com/docs/books/effective/  -Puce

Puce wrote: plutoisaplanet wrote:  // i probably should do an instanceof check, but its just a test...  But if you're interessted in how to write a good equals method, read chapter 3 of "Effective Java Programming Language Guide": http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf http://java.sun.com/docs/books/effective/  -Puce  wow thats intense lol.  and of course, its explicitly stated there:  ? Don?t substitute another type for Object in the equals declaration. It is not   uncommon for a programmer to write an equals method that looks like the fol-   lowing, and then spend hours puzzling over why it doesn?t work properly:     public boolean equals(MyClass o) {          ...     }   The problem is that this method does not override Object.equals, whose ar-   gument is of type Object, but overloads it instead (Item 26). It is acceptable to   provide such a ?strongly typed? equals method in addition to the normal one   as long as the two methods return the same result but there is no compelling   reason to do so. It may provide minor performance gains under certain circum-   stances, but it isn?t worth the added complexity (Item 37).


Related Links

Can java do everything?
Exception in thread "main" java.lang.StackOverflowError
Recommend Java UML modelling tool?
Beginning College Java Project. Help Me Please !!!
using the - key to stand for a string of characters
specifying conditions for a command line argument
Homedrive?
confused with exit method
How do I use Java
Combine java with DOS?
Using another class in Netbeans 5.0
array question?
Noob question, trying to set up java, netbeans not working
newbie
breaking long string into columns of data
please stop posting on the c# vs java thread, ITS DEAD OK!!!