使用Collections.min中的值进行索引时的IndexOutOfBoundsException

Mr *_*ker 2 java arraylist

我试图获取最小值的索引,但是当第一个元素的最小值为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);
Run Code Online (Sandbox Code Playgroud)

错误:

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) 
Run Code Online (Sandbox Code Playgroud)

gef*_*fei 11

int index = distanceList.get(min);你在一起的min-th元素distanceList.

试着int index = distanceList.indexOf(min);改为