我试图获取最小值的索引,但当第一个元素的最小值为 570(索引 = 0)时,我收到此错误。我做错了什么?
代码:
//ArrayList of distanceList [570, 621, 716, 906, 1055, 1253, 1314, 1314]
ArrayList<Integer> distanceList = entry.getValue();
//min is 570
int min = Collections.min(distanceList);
int index = distanceList.get(min);
错误:
threw exception [java.lang.IndexOutOfBoundsException: Index: 570, Size: 8] with root cause
java.lang.IndexOutOfBoundsException: Index: 570, Size: 8
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
最佳答案
使用int index = distanceList.get(min);
,您将获得distanceList
的第min
元素。
尝试int index = distanceList.indexOf(min);
而不是
关于java - 使用 Collections.min 中的值进行索引时出现 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32488682/