別のクラスから整数をランダムに取得しようとしていますが、それらの値を取得する方法がわかりません。最初のクラスは、最初の乱数ジェネレーターです。2 番目のクラスは、これらの数値を取得しようとしているクラスです。グラブ メソッドを作成しましたが、これらのランダムな整数をクラス ArrayBag から取得する方法がわかりません。どんな助けでも大歓迎です!
import java.util.Random;
public final class ArrayBag<T> implements BagInterface<T>
{
private final T[] bag;
private int numberOfEntries;
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 6;
private static final int MAX_CAPACITY = 10000;
/** Creates an empty bag whose initial capacity is 6. */
public ArrayBag()
{
this(DEFAULT_CAPACITY);
System.out.print("Generating 6 random integers 1 - 6 in ArrayBag\n");
//Random loop to generate random numbers 1 to 6
Random rand = new Random();
for(int start=1; start <=6; start++)
{
int randInt = rand.nextInt(6);
System.out.println("Generated : " + randInt);
}
System.out.println("Finished\n");
} // end default constructor
This is my second class and method...
import java.util.Random;
public class PopulationBag
{
public PopulationBag()
{
ArrayBag ab = new ArrayBag();
LinkedBag lb = new LinkedBag();
}
/**
* Grab Method
*/
public int grab()
{
ArrayBag ab = new ArrayBag();
Random grab = new Random();
for(int start = 1; start <= 6; start++)
{
int randGrab = grab.nextInt(ab);
System.out.println("Randomly grabbed values : " + randGrab);
}
}
}