“线程“main”java.lang.IndexOutOfBoundsException 中的异常:索引:0,大小:0”与 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26272508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0" with ArrayList?
提问by Jake Urban
"Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0" is the main error I get when I compile this method:
“线程“main”中的异常 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” 是我编译此方法时遇到的主要错误:
public static ArrayList<ArrayList<Integer>> createSparseArray(int len, double den) {
int counter = 0;
ArrayList<Integer> placeHolder = new ArrayList<Integer>();
for (int j = 0; j < len; j++) {
double randomNumber = Math.random();
if (randomNumber < den) {
counter++;
placeHolder.add(j);
}
}
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
for (int k = 0; k < counter; k++) {
for (int m = 0; m < 2; m++) {
list.get(0).set(placeHolder.get(k), (int) (Math.random() * (99999) + 1));
}
}
return list;
}
How can I fix this?
我怎样才能解决这个问题?
回答by Santhosh
ArrayList<ArrayList<Integer>> list
doesnt contain any element in (0)th postion and the compiler throws out of bounds exception on iterating when it doesn't find any element in the specified postion.
ArrayList<ArrayList<Integer>> list
在第 (0) 个位置不包含任何元素,并且编译器在迭代时在指定位置找不到任何元素时抛出越界异常。
when you try executing list.get(0).set(placeHolder.get(k), (int) (Math.random() * (99999) + 1));
statement , your list doesnt contain any element inside it. you need to iterate the inner list to set the values for the list
.
当您尝试执行list.get(0).set(placeHolder.get(k), (int) (Math.random() * (99999) + 1));
statement 时,您的列表中不包含任何元素。您需要迭代内部列表以设置list
.
回答by Vla
I thingk u have mistake here
我想你在这里有错误
list.get(0).set(placeHolder.get(k), (int) (Math.random() * (99999) + 1));
列表。get(0).set(placeHolder.get(k), (int) (Math.random() * (99999) + 1));
The java.util.ArrayList.get(int index) method returns the element at the specified position in this list.
java.util.ArrayList.get(int index) 方法返回此列表中指定位置的元素。
You are setting just to first element in the list arrray
您仅设置为列表数组中的第一个元素
回答by borgmater
Do not use list.set(index, element)
on a nonexistent element, since it replaces the old (nonexistent) element with a new one and returns the old one. Instead, use list.add(index, element)
and it will work.
不要list.set(index, element)
在不存在的元素上使用,因为它用新元素替换旧(不存在)元素并返回旧元素。相反,使用list.add(index, element)
它会起作用。
回答by vedant dave
Here, the main problem is Array indexing and real logic indexing difference...
在这里,主要问题是数组索引和真正的逻辑索引的区别......
Start with facts that ArrayList not startwith index 0
从ArrayList不以索引 0开始的事实开始
* but in your logic when you use X.add("Y")with i = 0 then logic can not find place 0 or can not recover position element 0; *
* 但是在您的逻辑中,当您使用X.add("Y")和 i = 0 时,逻辑无法找到位置 0 或无法恢复位置元素 0;*
that means ... it show indexing error of outbound;
这意味着......它显示出站索引错误;
solution ;
解决方案 ;
first solution : use i<istead of i<=second solution : use i = 1for initialization instead of i = 0;
第一溶液:使用我<的istead我<=第二溶液:使用I = 1初始化,而不是I = 0;