2
  • I have done something like this :

    public class MainActivity extends AppCompatActivity {
    
        ArrayList<String> al = new ArrayList<String>();
        ArrayList<String> a2 = new ArrayList<String>();
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            al.add("C");
            al.add("A");
            al.add("E");
            al.add("B");
            al.add("D");
            al.add("F");
    
            a2.add("C");
            a2.add("C");
            a2.add("E");
            a2.add("B");
            a2.add("D");
            a2.add("F");
    
    
            for (int i = 0; i < al.size(); i++) {
                for (int j = 0; j < a2.size(); j++) {
    
                    if (al.get(i).equals(a2.get(j))) {
                        a2.remove(j);
                        Log.e("array 2 ", a2.toString());
                        break;
                    }
    
                }
            }
         } 
      }
    

-- But my output is like this :

[C, E, B, D, F]
[C, B, D, F]
[C, D, F]
[C, F]
[C]

-- i am having same elements in arraylist 2, then also i am getting 'C', it should be null .i.e zero un-common value.

1
  • 2
    al.removeAll(a2) removes the common items between al and a2. Read more here Commented Mar 29, 2016 at 10:06

4 Answers 4

3

You are messing with the index when you remove the a2 items directly inside for loop. Refer to my solution below

        ArrayList<String> al = new ArrayList<>();
        ArrayList<String> a2 = new ArrayList<>();
        al.add("C");
        al.add("A");
        al.add("E");
        al.add("B");
        al.add("D");
        al.add("F");

        a2.add("C");
        a2.add("C");
        a2.add("E");
        a2.add("B");
        a2.add("D");
        a2.add("F");

        ArrayList<String> tempToDelete = new ArrayList<>();

        for (int i = 0; i < al.size(); i++) {
            for (int j = 0; j < a2.size(); j++) {
                if (al.get(i).equals(a2.get(j))) {
                    tempToDelete.add(a2.get(j));
                    break;
                }

            }
        }

        a2.removeAll(tempToDelete);

for shorter method, you can just do this:

a2.removeAll(al);
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much, I tried both method, First working fine for the object arraylist. your solution saved my time cheers :)
1

Try This it will help you

     ArrayList<String> al = new ArrayList<String>();
     ArrayList<String> a2 = new ArrayList<String>();

    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");

    a2.add("C");
    a2.add("C");
    a2.add("E");
    a2.add("B");
    a2.add("D");
    a2.add("F");

   Integer a = null;
 for (int i=0; i<a1.size(); i++)
  {
    a = a1.get(i);

    if (a2.contains(a)
{
    a2.remove(a);
    a1.remove(a);
    i--;
 }
 }

ArrayList<String> finaldata = new ArrayList<String>();
finaldata.addAll(a1);
finaldata.addAll(a2);
// finaldata = { A}

1 Comment

final is keyword in Java
0

Just replace break; with this:

j = -1;

code:

 for (int i = 0; i < al.size(); i++) {
        for (int j = 0; j < a2.size(); j++) {

            if (al.get(i).equals(a2.get(j))) {
                a2.remove(j);
                Log.e("array 2 ", a2.toString());
                j = -1;

            }

        }
    }

Comments

0

I recommend you to use Set's instead of ArrayList. You can find some info here.

Also, here you can read some examples of using java Set in set operations in this stackoverflow answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.