Check if an array is a subset of another array. Java

I need to make an array of 100 numbers, and then randomly shuffle the first 20 to have 2 different arrays; A and B.

For this assignment, I need to verify that the first 20 numbers from Array A are a subset of the first 20 numbers af Array B

So far I have this:

import java.util.Random; public class opgave6 { public static void main(String[] args){ Verzameling a = new Verzameling(20, 3); Verzameling b = new Verzameling(20, 4); System.out.println(Verzameling.deelverzamelingVan()); } } class Verzameling { int[] elementen; int elementen2; static int aantal2; Verzameling(int aantal , int seed) { elementen = new int[100]; int aantal2 = aantal; for(int i = 0; i < 100; i++){ elementen[i] = i; } Random random1 = new Random(seed); for(int i = 0; i < 100; i++){ int r = random1.nextInt(100); int temp; temp = elementen[i]; elementen[i] = elementen[r]; elementen[r] = temp; } printVerzameling(aantal); } Verzameling(int seed) { } void printVerzameling(int aantal){ for (int i = 0; i < aantal; i++){ System.out.print(elementen[i] + " "); } System.out.println(); } static boolean deelverzamelingVan() { while (true) { for(i = 0; i < aantal2; i++){ for(j = 0; j < aantal2; j++){ if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j]) break; } } } } } 

However, it does not work at all, because I cannot understand how to compare element [i] with object A with element [j] from object B. How to compare different elements of both objects using a static method in the same class.

(So, Verzameling A and B are instances of the Verzameling class with a static method to check if A is a subset of B. How can I get numbers in an array from Verzameling A and B?)

If something is unclear let me know! I don't need whole solutions, just how can I access the value of elementen [i] from objects A and B. thanks!

EDIT:

This is problem:

if (Verzameling.a.elementen [i] == Verzameling.b.elementen [j])

thanks for the comment, however, the error still compiles. It says that he cannot find the symbol about verzameling.a.elementen, i, verzameling.b.elementen and j. I think I called it wrong, is it okay to call a variable saying: classname.objectname.variable object?

+4
source share
4 answers

your if statement is hacked:

 if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j]) 

you are assigning, you are not testing anything.

to try

 Verzameling.a.elementen[i].equals(Verzameling.b.elementen[j]) 

or

 Verzameling.a.elementen[i] == Verzameling.b.elementen[j] 

note that you need to make sure Verzameling.a.elementen [i] is not null if you use the equals method. Also note that for numbers == is good, but for objects you want to use equal if you are not sure you want == (== means something very specific to objects)

PREEDIT -

Your code does not compile for several reasons. The first one is that your variables a and b are declared in the main scope, so they are only available mainly. Moving them to a class and making them public and static means that they can be accessed from anywhere.

EDIT - I can't believe I'm doing someone else's homework, but - it doesn't work, but at least it compiles

 import java.util.Random; public class opgave6 { public static Verzameling a = new Verzameling(20, 3); public static Verzameling b = new Verzameling(20, 4); public static void main(String[] args) { System.out.println("before something"); System.out.println(Verzameling.deelverzamelingVan()); System.out.println("after something"); } } class Verzameling { int[] elementen; int elementen2; static int aantal2; Verzameling(int aantal, int seed) { elementen = new int[100]; int aantal2 = aantal; for (int i = 0; i < 100; i++) { elementen[i] = i; } Random random1 = new Random(seed); for (int i = 0; i < 100; i++) { int r = random1.nextInt(100); int temp; temp = elementen[i]; elementen[i] = elementen[r]; elementen[r] = temp; } printVerzameling(aantal); } // you arent using it -- do so or delete it Verzameling(int seed) { } void printVerzameling(int aantal) { for (int i = 0; i < aantal; i++) { System.out.print(elementen[i] + " "); } System.out.println(); } static boolean deelverzamelingVan() { for (int i = 0; i < aantal2; i++) { for (int j = 0; j < aantal2; j++) { int aElementen = opgave6.a.elementen[i]; int bElementen = opgave6.b.elementen[j]; System.out.println(String.format("Comparing a[i] to b[i] [%d, %d]", aElementen, bElementen)); if (aElementen == bElementen) break; } } // need to return something, i dont know what. return true; } } 
+2
source
 if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j]) 

assigns, not compilation. I suppose you want ==, but evenbetter would have to override equals ()

 if(Verzameling.a.elementen[i] == Verzameling.b.elementen[j]) 

or

 if(Verzameling.a.elementen[i].equals(Verzameling.b.elementen[j])) 
+1
source

Verzameling.a.elementen [i] = Verzameling.b.elementen [j]

it should be:

 Verzameling.a.elementen[i] == Verzameling.b.elementen[j] 
0
source

Sort each of the arrays, compare. Or better yet, put the integers in the given type and check if it is a subset of the other. Find the Set interface, use the containsAll () method.

0
source

All Articles