Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking if array is subset of other array. Java
    primarykey
    data
    text
    <p>I have to make an array of 100 numbers and then shuffle the first 20 randomly to have 2 different arrays; A and B.</p> <p>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</p> <p>Up until now I have this:</p> <pre><code>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 &lt; 100; i++){ elementen[i] = i; } Random random1 = new Random(seed); for(int i = 0; i &lt; 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 &lt; aantal; i++){ System.out.print(elementen[i] + " "); } System.out.println(); } static boolean deelverzamelingVan() { while (true) { for(i = 0; i &lt; aantal2; i++){ for(j = 0; j &lt; aantal2; j++){ if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j]) break; } } } } } </code></pre> <p>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.</p> <p>(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?)</p> <p>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!</p> <p>EDIT:</p> <p>this is the problem line:</p> <p>if(Verzameling.a.elementen[i] == Verzameling.b.elementen[j])</p> <p>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? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload