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

前端 未结 6 1684
醉话见心
醉话见心 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:23

    Here's what I do. I find the minimum first then after the minimum is found, it is removed from ArrayList.

    ArrayList<Integer> a = new ArrayList<>();
    a.add(3);
    a.add(6);
    a.add(2);
    a.add(5);
    
    while (a.size() > 0) {
        int min = 1000;
        for (int b:a) {
            if (b < min)
                min = b;
        }
        System.out.println("minimum: " + min);
        System.out.println("index of min: " + a.indexOf((Integer) min));
        a.remove((Integer) min);
    }
    
    0 讨论(0)
  • 2020-12-02 20:24

    You have to traverse the whole array and keep two auxiliary values:

    • The minimum value you find (on your way towards the end)
    • The index of the place where you found the min value

    Suppose your array is called myArray. At the end of this code minIndex has the index of the smallest value.

    var min = Number.MAX_VALUE; //the largest number possible in JavaScript
    var minIndex = -1;
    
    for (int i=0; i<myArray.length; i++){
       if (myArray[i] < min){
          min = myArray[i];
          minIndex = i;
       }
    }
    

    This is assuming the worst case scenario: a totally random array. It is an O(n) algorithm or order n algorithm, meaning that if you have n elements in your array, then you have to look at all of them before knowing your answer. O(n) algorithms are the worst ones because they take a lot of time to solve the problem.

    If your array is sorted or has any other specific structure, then the algorithm can be optimized to be faster.

    Having said that, though, unless you have a huge array of thousands of values then don't worry about optimization since the difference between an O(n) algorithm and a faster one would not be noticeable.

    0 讨论(0)
  • 2020-12-02 20:26

    You can use Collections.min and List.indexOf:

    int minIndex = list.indexOf(Collections.min(list));
    

    If you want to traverse the list only once (the above may traverse it twice):

    public static <T extends Comparable<T>> int findMinIndex(final List<T> xs) {
        int minIndex;
        if (xs.isEmpty()) {
            minIndex = -1;
        } else {
            final ListIterator<T> itr = xs.listIterator();
            T min = itr.next(); // first element as the current minimum
            minIndex = itr.previousIndex();
            while (itr.hasNext()) {
                final T curr = itr.next();
                if (curr.compareTo(min) < 0) {
                    min = curr;
                    minIndex = itr.previousIndex();
                }
            }
        }
        return minIndex;
    }
    
    0 讨论(0)
  • 2020-12-02 20:30

    try this:

    public int getIndexOfMin(List<Float> 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;
    }
    
    0 讨论(0)
  • 2020-12-02 20:33

    There is an easier way to find a min integer in array list:

    int min = array.get(0);
            for (int i : array){
                min = min < i ? min : i;
            }
    
    0 讨论(0)
  • 2020-12-02 20:34

    This should do it using built in functions.

    public static int minIndex (ArrayList<Float> list) {
      return list.indexOf (Collections.min(list)); }
    
    0 讨论(0)
提交回复
热议问题