Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Complex comparison of two lists

807580Feb 4 2010 — edited Feb 6 2010
Hi guys,

Let’s see if you can help me with this one...
I need to compare two lists of numbers that should contain the same values one by one, but obviously that’s not the case because sometimes one of the sources misses a value. I must log when a discrepancy is found.

list1: [5,4,6,7,10]
list2: [5,6,7,9,10]

As you can see, the list2 misses the value 4, and list1 misses the value 9.

Elements are not ordered but there are never two identical values in a row:
Impossible: [4,5,5,6]
Possible: [4,5,6,5]


My first idea is to go through the list one by one. Here some pseudo-code:
while (!list1.isEmpty() && !list2.isEmpty()){
       if (list1[0] = list2[0]) then 
              list1.remove(0)
              list2.remove(0)
       else (list1[0] != list2[0]) then 
           LOG ERROR but who miss the value?
            now what? 
My problem is that I don’t know what to do when two values don’t match, because in that moment I don’t know if the mismatch is because the value in list1 is not in list2, or the other way round.

One option is to check if the next value in one of the list is the current one in the other, but what if there are more than 1 value missed?

Your help would be much appreciated. Thanks

Edited by: pedroruiz on Feb 4, 2010 7:53 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 6 2010
Added on Feb 4 2010
17 comments
797 views