如何创建从另一个类中获取随机值的方法?

时间:2015-01-29 17:45:12

标签: java random remove-if bag

我试图从另一个类中随机抓取整数,但无法弄清楚如何获取这些值。第一类是我的初始随机数生成器。第二类是我试图将这些数字检索到的类。我已经创建了一个抓取方法,但无法弄清楚如何从类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);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

据我所知,你要做的是建立一个生成并保存用户给定数量的随机int和第二类的类,它可以访问第一类生成的值。

我注意到你的方法存在一些问题。首先,您正在生成随机数,但无论如何都不会存储它们。如果用户给出了随机int的数量,你可以尝试使用ArrayList方法,或者使用一个新的Java 8方法,使用Random类的ints()方法(我在下面的代码中使用)。您还需要一个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;
        }