I have to make an array of 100 numbers and then shuffle the first 20 randomly to have 2 different arrays; A and B.
For this assignment I have to check wether the first 20 numbers from Array A are a subset of the first 20 numbers af Array B
Up until now 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 doesnt work at all because I cannot figure out how to compare the elementen[i] from object A to element[j] from object B. How can i compare the different elements from both objects by using the static method in the same class.
(So Verzameling A and B are both instances of the Verzameling class, with a static method to check if A is subset of B. How can I get the numbers in the array from Verzameling A and B?)
If something is not clear please let me know! I don't need whole solutions, just how I can access the value of elementen[i] from object A and B. thanks!
EDIT:
this is the problem line:
if(Verzameling.a.elementen[i] == Verzameling.b.elementen[j])
thanks for the comment, however it is still erroring when i compile. It says it cannot find symbol about verzameling.a.elementen, i, verzameling.b.elementen and j. I think i am naming it wrong, is it ok to call the variable by saying: classname.objectname.variable of object?
=; it shuld be==. But then, it wouldn't compile, so that probably doesn't help you.Arrays.asListdoesn't work like you expect when given a primitive array.List<int[]> list = Arrays.asList(someIntArray)