• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist 'contains' method  RSS feed

 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList which is filled in with multiple Strings.
However,when I use the 'contains' method to search if a given string exists in the arraylist,I do not get the desired result.
Why is it so - i thought the 'contains' method uses 'equals' to compare by value.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the source code for ArrayList, I see that it does indeed use equals(). Therefore the reason you dont get the expected result must reside in your code.

What are you expecting and what are you getting?

Bill
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us some code; maybe we can tell you what's going on.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know the best place to look for the code that makes up the jdk? Is this for ArrayList for example? https://openjdk.dev.java.net/source/browse/openjdk/jdk/trunk/j2se/src/share/classes/java/util/
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know the best place to look for the code that makes up the jdk?

A source archive is bundled with your jdk. Check your JAVA_HOME for a file called src.zip.
 
Minh Tran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In contains(Object obj);

o==null ? e==null : o.equals(e) from java.util.AbstractCollection

[ June 13, 2007: Message edited by: Minh Tran ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!