3

I am trying to compare two ArrayLists, and after comparision i have to get common elements between those two arraylists and show them in a third arraylist.

This is my code, here newList is the arraylist in which i want to add the common elements but each time i am adding elements in this arraylist it is showing only the last element.

            ArrayList<String> list2 = new ArrayList<String>();
    list2.add("1");
    list2.add("abc");
    list2.add("3");
    list2.add("4");
    ArrayList<String> list1 = new ArrayList<String>();
    list1.add("3");
    list1.add("4"); list1.add("7");
    list1.add("8");

    list1.add("12");
    list1.add("4");
    list1.add("53");
    list1.add("2");
    list1.add("62");
    list1.add("abc");

    System.out.println("btn click r_answer "+list1+" "+list2);

     for (int i=0;i<list1.size();i++) {
            for (int j=0;j<list2.size(); j++) {
                if(list1.get(i).equals(list2.get(j)))
                    System.out.println("equals..:"+list2.get(j));
                         newList.add(list2.get(j));
                }
            }
Vaishali Sharma
  • 602
  • 3
  • 6
  • 24
  • have you initialized newlist? – Rat-a-tat-a-tat Ratatouille Feb 21 '14 at 07:18
  • yea obviously.. private ArrayList newList=new ArrayList(); – Vaishali Sharma Feb 21 '14 at 07:19
  • what is the so called issue ? you getting any crash ? – Triode Feb 21 '14 at 07:20
  • "each time i am adding elements in this arraylist it is showing only the last element." Only the last common element is adding in newList – Vaishali Sharma Feb 21 '14 at 07:21
  • post the full code from here it looks to me you are re initializing the list again – Triode Feb 21 '14 at 07:24
  • Are you saying that the `"equals..:"` message is only getting printed once? Or are you saying that the list only has one element at the end of the method? If it's the latter, can you tell us how you checked what was in the list? Also, are you running this on a button click or something? Are you sure you're only getting a single event, not multiple events? Does it happen on the event dispatch thread, or are you making new threads to run this? So many questions! It would be really nice if you could post your entire program. – Dawood ibn Kareem Feb 21 '14 at 08:41

3 Answers3

5

keep braces after if condition in for loop...

for (int i=0;i<list1.size();i++) {
        for (int j=0;j<list2.size(); j++) {
            if(list1.get(i).equals(list2.get(j))){
                System.out.println("equals..:"+list2.get(j));
                newList.add(list2.get(j));
            }
        }
    }
Anil kumar
  • 1,534
  • 3
  • 14
  • 20
1

try this... An easy way to get common elements in two ArrayLists

    ArrayList<String> list2 = new ArrayList<String>();
    // add the elements into list2
    ArrayList<String> list1 = new ArrayList<String>();
    // add the elements into list1

    ArrayList<String> newList = new ArrayList<String>(list1);
    newList.retainAll(list2);

    // Now newList will contain common elements
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0
listA.retainAll(listB);
// listA now contains only the elements which are also contained in listB.
If you want to avoid that changes are being affected in listA, then you need to create a new one.

List<Integer> common = new ArrayList<Integer>(listA);
common.retainAll(listB);
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34