return an ArrayList of Integers that consist of n random numbers?

后端 未结 5 602
夕颜
夕颜 2021-01-17 05:17

How do I create the method RandomArray and let it take in an integer n and return an ArrayList of Integers that consist of n random numbers between 0 and 255.(in other words

5条回答
  •  猫巷女王i
    2021-01-17 05:42

    You mean something like this:

    public ArrayList randomArrayList(int n)
    {
        ArrayList list = new ArrayList<>();
        Random random = new Random();
    
        for (int i = 0; i < n; i++)
        {
            list.add(random.nextInt(255));
        }
        return list;
    }
    

提交回复
热议问题