Results 1 to 4 of 4
  1. #1
    prasad.java97 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Post 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 ?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  3. #3
    prasad.java97 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default 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");
    			}
    		}
    
    	}
    
    }
    Thanks and Regards,
    Prasad Reddi

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Comparing two ArrayLists elements but getting in correct result.

    I guess I missed what the objective of the program is.

    Is the program supposed to find if the last element in the first list is in the second list?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. show elements in correct order
    By Shien in forum New To Java
    Replies: 4
    Last Post: 12-30-2011, 05:20 PM
  2. Replies: 2
    Last Post: 10-25-2011, 10:26 PM
  3. How to move elements between Arraylists
    By Kerrai in forum New To Java
    Replies: 2
    Last Post: 11-04-2010, 12:44 PM
  4. how to compare the elements of the two arraylists al1,al2
    By raj reddy in forum Web Frameworks
    Replies: 33
    Last Post: 11-25-2009, 05:48 PM
  5. how to compare the elements of these two arraylists
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 03-25-2009, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •