File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How I can compare an arraylists values in order? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How I can compare an arraylists values in order?" Watch "How I can compare an arraylists values in order?" New topic

How I can compare an arraylists values in order?

D Hagy

Joined: Jul 05, 2011
Posts: 12
I need to have two arraylists. I declared them. I declared a "mine" arraylist and an "user" arraylist with this code:

I want to store in these values, what are will be important to contact with the user:
I added values to my mine arraylist:

I want to add a values to the user named arraylist if the user press a button. So I added this code:

to an another button.

I added two line to the buttons so I can follow how the arraylist looks like in the appropriate moments.

Then if the user clicks an another button, I want to compare these lists, so I implemented the following code:

If the two arraylists are the same in the order and in values, I want to show a new screen (its name is for example good), and if not I want to show an another( its name is for example lose). But I have a problem. My program is only show my lose screen if the user only clicked the button which add 1 to the arraylists, or if he/she only clicked the button which add 0 to the arraylist. How can I make my program to run as I want? Thanks in advance
Peter Johnson

Joined: May 14, 2008
Posts: 5852

Have your tried stepping through the for loops to see what is going on? With only 3 values in the arrays (for your example), you should be able to desk-check the loops and immediately see what the problem is. Actually, you don't even have to go very far: the first time through the outer loop, you go through the inner loop 3 times. The first time is ok, but what happens the second time through the inner loop?

That should be enough of a hint. But let us know if you still don't see it.

JBoss In Action
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Why are you using the == operator on a reference type? That almost always gives the wrong result.
I agree. Here's the link:
subject: How I can compare an arraylists values in order?
It's not a secret anymore!