0

別のクラスから整数をランダムに取得しようとしていますが、それらの値を取得する方法がわかりません。最初のクラスは、最初の乱数ジェネレーターです。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);
        }
    }

}
4

1 に答える 1

0

あなたがやろうとしていることを理解している限り、ユーザーが指定した数のランダムな整数を生成して保持するクラスと、最初のクラスによって生成された値にアクセスできる2番目のクラスを構築することです。

あなたのアプローチにはいくつかの問題があることに気付きました。まず、乱数を生成していますが、とにかくそれらを保存しないでください。ランダム int の数がユーザーによって指定されている場合は、ArrayList アプローチを試すか、Random クラスのメソッド ints() を使用して新しい Java 8 アプローチを使用できます (以下のコードで使用しています)。また、プライベート変数へのアクセスを可能にする getter メソッドも必要です。

次のコードを検討してください。

public class ArrayBag {
        private int[] randomBag;
        private boolean initialized = false;
        private static final int DEFAULT_CAPACITY = 6;
        private static final int MAX_CAPACITY = 10000;

        public ArrayBag() {
            Random random = new Random();

            randomBag = new int[DEFAULT_CAPACITY];

            // The method ints introduced in Java 8 accepts three params:
            // number of ints generated, upper and lower bound between which the ints will be generated.
            // toArray method passes the generated nums as an dynamically created array,
            // which you can assign to a variable.
            randomBag = random.ints(DEFAULT_CAPACITY, 1, DEFAULT_CAPACITY + 1).toArray();
        }

// the 'getter' method
        public int getRandomInt(int i) {
            return randomBag[i];

        }
    }
        /* Second class */
        public class PopulationBag {
        private ArrayBag ab = new ArrayBag();
        private Random grab = new Random();

       // Are the numbers form the ArrayBag going to be grabbed at random,
     //or simply one after another?

        public int[] grabInOrder(int numberOfGrabbedInts) {
    // Create an array to hold ints.
            int[] intBag = new int[numberOfGrabbedInts];
    // Fill in data to the intBag
            for (int i = 0; i < numberOfGrabbedInts; i++) {
    // Call getRandomInt() method form ArrayBag class
     intBag[i] = ab.getRandomInt(i);
                System.out.println("Randomly grabbed values : " + intBag[i]);
            }

            return intBag;
        }

    private int[] grabAtRandom(int numberOfGrabbedInts) {
            int[] intBag = new int[numberOfGrabbedInts];
            for (int i = 0; i < numberOfGrabbedInts; i++) {
                // This is kind of funky, basically it returns at random the ints saved in ArrayBag.
                intBag[i] = ab.getRandomInt(grab.ints(1, 1, numberOfGrabbedInts).findFirst().getAsInt());
                System.out.println("Randomly-randomly grabbed values : " + intBag[i]);
            }
            return intBag;
        }
于 2015-01-29T20:31:55.200 に答える