70

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 of the smallest float so I can use that index number elsewhere in my code. I'm a beginner, so please don't hate me. Thanks!

1
  • I think the chose solution is incomplete. it is still missing something! What if the min number is present multiple times? It is after all a list. So I think it should return a List if indices for the min value Commented Aug 10, 2013 at 19:37

8 Answers 8

112

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Your solution is still missing something! What if the min number is present multiple times? It is after all a list. So I think you should return a List if indices for the min value
@AdelBoutros If that is what the OP wants, this solution can be taken as the starting point and be improved upon.
@MarimuthuMadasamy Thanks bro..you are the hero..,master maind
Doesn't this iterate the list twice?
16

This should do it using built in functions.

public static int minIndex (ArrayList<Float> list) {
  return list.indexOf (Collections.min(list)); }

Comments

10

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;
}

4 Comments

Float.compare(f.floatValue(), min) < 0 can be changed to f < min. And min = f.floatValue() to min = f. Wrapper classes get cast implicitly to their primitive equivalents.
@Dukeling,you mean f<=min?
No, I mean f < min (from the docs of Float.compare: "returns the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2"). Unless you mean Float.compare(f.floatValue(), min) <= 0 (which would be f <= min).
@Dukeling delegating to Float.compare will avoid erroneous behavior of comparing NaN
8

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;
        }

1 Comment

And what about the index?
4
public static int minIndex (ArrayList<Float> list) {
  return list.indexOf (Collections.min(list));
 }
System.out.println("Min = " + list.get(minIndex(list));

Comments

0
  1. Declare a arraylist with Floats.

  2. Collection.min() - finding the minimum element in the list.

  3. List.indexOf() - finding the index of the minimum element.

public class Test {

    public static void main(String[] args) {

        ArrayList<Float> ary = new ArrayList<Float>();
        ary.add((float) 3.0);
        ary.add((float) 6);
        ary.add((float) 2);
        ary.add((float) 1.3);
        ary.add((float) 4.2);
        int indx = minIndex(a);
        System.out.println(indx);
    }

    public static int minIndex(ArrayList<Float> list) {
        return list.indexOf(Collections.min(list));
    }

}

Comments

-1

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.

Comments

-1

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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.