Results 1 to 4 of 4
 10272013, 12:58 PM #1Member
 Join Date
 Oct 2013
 Posts
 2
 Rep Power
 0
Comparing two ArrayLists elements but getting in correct result.
Hello All,
I am traing to comparing two list elements on below case's
But getting incorrect resul for case3.
List<Integer> a1 = new ArrayList<Integer>();
List<Integer> a2 = new ArrayList<Integer>();
Case1:
a1.add(1);
a1.add(2);
a1.add(3);
a1.add(4);
a1.add(5);
a1.add(6);
a2.add(1);
a2.add(2);
a2.add(3);
a2.add(4);
a2.add(5);
a2.add(6);
Result :Working correct!
Case2:
a1.add(1);
a1.add(2);
a1.add(3);
a1.add(4);
a1.add(5);
a1.add(6);
a2.add(7);
a2.add(8);
a2.add(9);
a2.add(10);
a2.add(11);
a2.add(12);
Result :Working correct!
Case2:
a1.add(1);
a1.add(2);
a1.add(3);
a1.add(4);
a1.add(5);
a1.add(6);
a2.add(1);
a2.add(3);
a2.add(4);
Result :Not Working
Getting our put like below:
Both are equal
Both are equal
Both are equal
Both are equal
Both are equal
Both are equal
But expected here is 3 results is equal and 3 results is not equal
for (Integer fistList : a1) {
for (Integer secondList : a2) {
if (fistList == secondList) {
matchFound = true;
break;
}
}
if (matchFound) {
System.out.println("Both are equal");
} else {
System.out.println("Both are not equal");
}
}
Please any one suggest what is the mistk in above code ?
 10272013, 02:10 PM #2
Re: Comparing two ArrayLists elements but getting in correct result.
Please edit your post and wrap your code with code tags:
[code]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
The logic needs to look at ALL of the elements of the arraylists BEFORE deciding that the contents of the two lists are the same. How many items does the code look at before the boolean is set to true.
A technique: assume the lists are equal, look at each pair of elements and fail the test at the first pair that are not equal.
If you don't find a mismatch after looking at the whole list, then your assumption was correct.If you don't understand my response, don't ignore it, ask a question.
 10272013, 07:09 PM #3Member
 Join Date
 Oct 2013
 Posts
 2
 Rep Power
 0
Re: Comparing two ArrayLists elements but getting in correct result.
Finally i solved my problem below are correct code.
Java Code:import java.util.ArrayList; public class TwoListCompare { public static void main(String[] args) { ArrayList<Integer> aList1 = new ArrayList<Integer>(); ArrayList<Integer> aList2 = new ArrayList<Integer>(); // boolean matchFound = false; //Commented to fix the problem // Add elements to List1 aList1.add(1); aList1.add(2); aList1.add(3); aList1.add(4); aList1.add(5); aList1.add(6); aList2.add(4); aList2.add(21); aList2.add(6); for (Integer list1 : aList1) { boolean matchFound = false; // Added after fix for (Integer list2 : aList2) { if (list1 == list2) { matchFound = true; break; } } if (matchFound) { System.out.println("Both elements are equal"); } else { System.out.println("Both elements are not equal"); } } } }
Prasad Reddi
 10272013, 07:57 PM #4
Similar Threads

show elements in correct order
By Shien in forum New To JavaReplies: 4Last Post: 12302011, 06:20 PM 
Why is a negative percent not giving me the correct result?
By MXA92 in forum New To JavaReplies: 2Last Post: 10252011, 11:26 PM 
How to move elements between Arraylists
By Kerrai in forum New To JavaReplies: 2Last Post: 11042010, 01:44 PM 
how to compare the elements of the two arraylists al1,al2
By raj reddy in forum Web FrameworksReplies: 33Last Post: 11252009, 06:48 PM 
how to compare the elements of these two arraylists
By raj reddy in forum Web FrameworksReplies: 1Last Post: 03252009, 11:55 PM
Bookmarks