0

Suppose I have array A : A = {1, 2, 3, 4, 5, 6, 7, 8, 9}

And then I used a scanner to input my preferences to another array, suppose B, and then B has values : B = {1, 2, 6, 8, 9}

Now the question is, I have another array (C), is there a way so when array B is set then array C automatically has values : C = {3, 4, 5, 7}

Which C's values is the rest of A's that B doesn't pick. I'm using Java Language. Any help is appreciated.

Rayan Suryadikara
  • 237
  • 1
  • 3
  • 17

3 Answers3

1

Below is the code how I have solve the same problem in python. import numpy a = numpy.array([1,2,3,4,5]) # declare array b = numpy.array([1,2,3]) # declare array c = set(a) - set(b)

Output:- c = [4,5]

Lavanya Pant
  • 702
  • 7
  • 23
  • oh right, I forgot to mention that my problem is with Java, do you know the function in Java, specifically netbeans? – Rayan Suryadikara Jun 05 '16 at 09:16
  • Yes you can do it in same way in JAVA using `java.util.Set`. Set Class have methods like union, intersection. You can check this [link](http://www.java2s.com/Code/Java/Collections-Data-Structure/Setoperationsunionintersectiondifferencesymmetricdifferenceissubsetissuperset.htm) for reference. – Lavanya Pant Jun 05 '16 at 12:04
1

Set operations are standardised in Java using TreeSet.

Set<Integer> a = new TreeSet<Integer>(Arrays.asList(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9}));
Set<Integer> b = new TreeSet<Integer>(Arrays.asList(new Integer[]{1, 2, 6, 8, 9}));

//union
Set<Integer> c = new TreeSet<Integer>(a);
c.addAll(b);
System.out.println(c);

//intersection
Set<Integer> d = new TreeSet<Integer>(a);
d.retainAll(b);
System.out.println(d);

//difference
Set<Integer> e = new TreeSet<Integer>(a);
e.removeAll(b);
System.out.println(e);

//reverse
List<Integer> list = new ArrayList<Integer>(a);
java.util.Collections.reverse(list);
System.out.println(list);

Source

Community
  • 1
  • 1
Jaydev
  • 1,794
  • 20
  • 37
1

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

Sweeper
  • 213,210
  • 22
  • 193
  • 313