How to find the minimum value in an ArrayList, along with the index number? (Java)

前端 未结 6 1709
醉话见心
醉话见心 2020-12-02 20:05

I need to get the index value of the minimum value in my arraylist in Java. MY arraylist holds several floats, and I\'m trying to think of a way I can get the index number o

6条回答
  •  一个人的身影
    2020-12-02 20:30

    try this:

    public int getIndexOfMin(List data) {
        float min = Float.MAX_VALUE;
        int index = -1;
        for (int i = 0; i < data.size(); i++) {
            Float f = data.get(i);
            if (Float.compare(f.floatValue(), min) < 0) {
                min = f.floatValue();
                index = i;
            }
        }
        return index;
    }
    

提交回复
热议问题