gpt4 book ai didi

java - 使用 Collections.min 中的值进行索引时出现 IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-02 13:48:33 25 4
gpt4 key购买 nike

我试图获取最小值的索引,但当第一个元素的最小值为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com