Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing two ArrayLists

 
Haani Naz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

i'm not sure if this has already been covered, my search didn't really answer my question so figured i'll post it anway..apologies if its already been covered - if you can direct me, that'd be great!

i'm basically trying to compare two ArrayLists.

example:

the program i'm trying to create works well, "should work" in the following way..

step 1: get user input which is stored in ArrayList userInput
step 2. check if ArrayList hits (will be explained) has any elements from
userInput
step 3: if hits is equal to an element of userInput goto step1

hits will store certain values which needs to be compared to the elements in userInput. and if it exist, new input is needed else the program can continue.

i declared hits and userInput as ArrayList<String> objects and input is accepted in String format.

i.e:
hits : [0,4,6,7] << numbers are in string format
userInput:[1,6,8,9,0]

so at the beginning of the program (as above) hits should compare all its values to userInput.

i have tried hits.equals(userInput) which always returns "false" (i suspect its only comparing the two first elements?)

i did also try hit.contains(userInput) - same result.


can somebody show me how this is done correctly? as you can see i'm very new to java..thanks!
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried hits.equals(userInput) which always returns "false" (i suspect its only comparing the two first elements?)

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Think the definition is pretty clear .... Keywords to notice -- ordered
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and welcome to javaranch
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you storing in the list and have you overridden the equals() method
to compare the list entries? When the user completes input for each list item,
you can use a for-each loop to check for it in the list.

Jim ... ...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell precisely from your description what the nature of the comparison should be; but between the methods of the List interface itself, and the java.util.Collections utility class, you should be able to do whatever you want in a single line of code.

For example, to test whether hits and userInput have any elements in common:



To test whether every element in userInput is in hits, you might say:



There are plenty of other interesting methods in the Collections class and the List interface, so spend a few minutes reading through the javadoc.
 
Haani Naz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers for your replies guys! i'll check out collections! ..as a temporary fix i dumped hits and userInput ArrayLists into Arrays and compared the content as strings which seems to work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic