我想查找保存最大值的索引/索引ArrayList
。我想保留数字的顺序(换句话说,不要排序),因为我想跟踪哪个索引具有什么价值。这些值来自一个随机数发生器,并且有两个(或更多)索引共享相同的最大值的可能性。在ArrayList中查找最大数量(可能有一个以上的最大值)
一个例子ArrayList
:
12,78,45,78
0,1,2,3 < - 指数
(所以指数,1和3包含我想要保持索引1和3的值为78的事实。我不想只创建一个新的ArrayList
,并且索引0和1的新的ArrayList
具有值78)
因此,我想找到所有具有最大值的索引,因为如果有多个索引,我将与他们一起“断开”连接。那么,如何找到包含最大值的指数并维持指数与价值的关系呢?
我写了下面的方法:
public static ArrayList<Integer> maxIndices(ArrayList<Integer> numArrayList) {
// ???
return numArrayList;
}
public static void removeElement(ArrayList<Integer> numArrayList, int index) {
numArrayList.remove(index);
}
public static int getMaxValue(ArrayList<Integer> numArrayList) {
int maxValue = Collections.max(numArrayList);
return maxValue;
}
public static int getIndexOfMaxValue(ArrayList<Integer> numArrayList, int maxVal) {
int index = numArrayList.indexOf(maxVal);
return index;
}
它看起来像你基本上没有编写任何代码执行请求的任务。 [你试过什么?](http://whathaveyoutried.com) –
@Matt Ball这就是我问的原因。我需要帮助如何做到这一点。我已经考虑过散列... –
如果它必须是一个List,那么就不需要声明ArrayList >的习惯,然后将它声明为List。 – BevynQ