OK, this is quite simple. Suppose you have these arrays
private final int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
private int[] b; // I didn't set b to any value
private Integer[] c; // REMEMBER to set it yourself or you will have a null pointer
Now we create a method that sets b
:
private void setB(int[] newB) {
this.b = newB;
}
Now every time you set the value of B...
b = someMethodThatGetsUserInput();
You should NOT use the above, instead, you call:
setB(someMethodThatGetsUserInput());
Why? Because we'll write some code that changes C in setB
. So every time you set the value of B, C is also changed.
Let's see how can we write the code to change C so that it is the elements that A has but B does not have.
The straightforward way is:
ArrayList<Integer> list = new ArrayList<>();
for (int i : a) {
if (!b.contains(a)) {
list.add(i);
}
}
c = list.toArray(new Integer[0]);
Easy!
There are other ways though, like using a set as others have mentioned. But I think you, as a beginner, should learn the most straightforward and a little inefficient way first.
Premature optimization is the root of all evil