[英]Java ArrayList IndexOutOfBoundsException Index: 1, Size: 1
[英]arraylist indexoutofboundsexception index 0 size 0
只是想知道为什么在matches.set(count,size)上运行基本上是深度优先的搜索算法后为什么得到indexoutofboundsexception?
当我尝试在arraylist的位置0运行它时出现错误,在默认索引为0时看起来很奇怪。
public void findInheritance(HashMap<String, ArrayList<String>> h, ArrayList<String> a) {
Queue<String> search = new LinkedList();
ArrayList<Integer> matches = new ArrayList<Integer>();
int count = 0;
String toBeSearched;
int size = 0;
for (String key: a) {
size = 0;
if (h.get(key).size() > 0 || h.get(key) == null) {
search.addAll(h.get(key));
while (search.size() > 0) {
size++;
toBeSearched = search.poll();
if (h.get(toBeSearched).size() > 0) {
search.addAll(h.get(toBeSearched));
}
}
}
matches.set(count, size);
count++;
}
int smallcount = 0;
for (String b: a) {
System.out.println(b);
System.out.println(matches.get(smallcount));
smallcount++;
}
}
ArrayList<Integer> matches = new ArrayList<Integer>();
[...]
matches.set(count, size);
该列表为空,因此索引count
没有任何内容,因此无法替换。
也,
if (h.get(key).size()>0 || h.get(key) == null) {
这行是错误的:如果h.get(key)
可以为null,则将导致NullPointerException。 您还应该调用一次get()
并将结果存储在变量中。
哦,我的话。 我使用.set而不是.add,并且ArrayList为空。 我的错。 感谢您的快速回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.