Java错误意想不到的类型要求:变量;发现:在一个ArrayList值 [英] Java Error unexpected type required: variable; found: value in an ArrayList
问题描述
我试图与分配元素大小随机的ArrayList数组,填充
0到100之间的随机值
I am trying to allocate a random ArrayList array with size elements, fill with random values between 0 and 100
这是我一直在收到错误块
This is the block that I keep getting the error in
public static ArrayList<Integer> generateArrayList(int size)
{
// Array to fill up with random integers
ArrayList<Integer> rval = new ArrayList<Integer>(size);
Random rand = new Random();
for (int i=0; i<rval.size(); i++)
{
rval.get(i) = rand.nextInt(100);
}
return rval;
}
我试过.SET和不用彷徨方法,但他们都不似乎工作
I've tried the .set and .get methods but neither of them seem to work
我不断收到所需的错误意想不到的类型:变量;发现:值
I keep getting the error unexpected type required: variable; found: value
这是在不用彷徨引发错误(我)
It is throwing the error at .get(i)
推荐答案
替换
rval.get(i) = rand.nextInt(100);
与
rval.add(rand.nextInt(100));
另外,为
循环将迭代0次的 rval.size()
被使用,因为列表最初空。它应该使用参数尺寸
来代替。当您使用初始化新的ArrayList&LT名单;整数GT;(大小)
,你只设置它的初始容量的。该列表仍是空的那一刻。
Also the for
loop will iterate zero times when rval.size()
is used because the list is initially empty. It should use the parameter size
instead. When you initialize the list using new ArrayList<Integer>(size)
, you are only setting its initial capacity. The list is still empty at that moment.
for (int i = 0; i < size; i++)
这篇关于Java错误意想不到的类型要求:变量;发现:在一个ArrayList值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!