2

I have the following arraylist

cd1 = [3,5,7,10,15,16]
cd2 = [4,10,5,8]

The output

[3,7,15,16]

as you can see one has 6 positions and the other 4.

then if they are repeated they should be stored in another ArrayList

Code I carry:

import java.util.ArrayList;

public class inexistentes {
    
     public static ArrayList<Integer> inexistentes(ArrayList<Integer> cd1, ArrayList<Integer> cd2){
        ArrayList<Integer> newList = new ArrayList<>();
        
        for(int i = 0; i < cd1.size(); i++){
            
            if(cd1.contains(i) ){
                newList.add(i);
            }
            
       }
         
         return newList;
     }
}

how can i buy those chains...

I already have a for but I don't know how to implement the if.

4
  • Why not make them both arrays then do this: Arrays.equals(arr1, arr2) Commented Aug 20, 2021 at 19:34
  • You're checking whether the first list contains an index. What you want to do is get the element at that index -- e.g., with cd1.get(i) -- and see if it's in the second list. Commented Aug 20, 2021 at 19:52
  • You can also explore apache common utils subtract. commons.apache.org/proper/commons-collections/apidocs/org/… Commented Aug 21, 2021 at 2:49
  • then if they are repeated they should be stored in another ArrayList This statement has some problems. What if the lists are reversed? Then you end up with [4,8]. How would you do this if the lists had to be processed without knowing the contents of either? Imo, what you probably want is List.retainAll which will give [5,10] no matter how you apply it. Then you can remove those from both lists. But that has problems too. What do you do if each list multiple of the same but in different numbers in each list? Are all considered duplicates. Is it just a one-to-one thing? – Commented Sep 9, 2021 at 19:25

3 Answers 3

7

You do not need to apply complex logic. Your requirement can be met easily by using the List#removeAll function.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> cd1 = List.of(3, 5, 7, 10, 15, 16);
        List<Integer> cd2 = List.of(4, 10, 5, 8);
        List<Integer> result = new ArrayList<Integer>(cd1);
        result.removeAll(cd2);
        System.out.println(result);
    }
}

Output:

[3, 7, 15, 16]

However, if you still want to do it your way, do it as follows:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> cd1 = List.of(3, 5, 7, 10, 15, 16);
        List<Integer> cd2 = List.of(4, 10, 5, 8);
        System.out.println(inexistentes(cd1, cd2));
    }

    public static ArrayList<Integer> inexistentes(List<Integer> cd1, List<Integer> cd2) {
        ArrayList<Integer> newList = new ArrayList<>();

        for (int i = 0; i < cd1.size(); i++) {
            int n = cd1.get(i);
            if (!cd2.contains(n)) {
                newList.add(n);
            }
        }

        return newList;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I appreciate it, I just have to do it as I showed with arrayList and with the method called "nonexistent".
@MaicolRojas - I've added an update to do it using your way.
And third way: List < Integer > result = cd1.stream().filter( i -> ! cd2.contains( i ) ).toList() ;
1

please try this loop

...

    for (int i : cd1) {

        if (!cd2.contains(i)) {

            newList.add(i);

        }

...

it returns [3, 7, 15, 16]

-- edit --

import java.util.ArrayList;

import java.util.Arrays;

public class inexistentes {

public static ArrayList<Integer> inexistentes(ArrayList<Integer> cd1, ArrayList<Integer> cd2) {

    ArrayList<Integer> newList = new ArrayList<>();

    for (int i : cd1) {

        if (!cd2.contains(i)) {

            newList.add(i);

        }

    }

    return newList;
}

public static void main(String[] args) {

    ArrayList cd1 = new ArrayList(Arrays.asList(3, 5, 7, 10, 15, 16));

    ArrayList cd2 = new ArrayList(Arrays.asList(4, 10, 5, 8));

    // [3,7,15,16]

    ArrayList cd3 = inexistentes(cd1, cd2);

    System.out.println(cd3.toString());

}

Comments

0

If you can use Set you can convert cd2 into a set and compare set with cd1. This would be quicker when compared to using contains() from list. At the same time Set uses more memory than List.

private static ArrayList<Integer> inexistentes(List<Integer> cd1, List<Integer> cd2) {
        ArrayList<Integer> newList = new ArrayList<>();
        Set<Integer> cd2Set = new HashSet<Integer>(cd2);
        for (int set1 : cd1) {

            if (!cd2Set.contains(set1)) {
                newList.add(set1);
            }

        }

        return newList;
    }

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.