user2130496
user2130496

Reputation: 707

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

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!

Upvotes: 70

Views: 224739

Answers (8)

Indhuja
Indhuja

Reputation: 82

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

}

Upvotes: 0

M E S A B O
M E S A B O

Reputation: 953

public static int minIndex (ArrayList<Float> list) {
  return list.indexOf (Collections.min(list));
 }
System.out.println("Min = " + list.get(minIndex(list));

Upvotes: 4

Arwan Khoiruddin
Arwan Khoiruddin

Reputation: 454

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

Upvotes: -1

Marimuthu Madasamy
Marimuthu Madasamy

Reputation: 13531

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

Upvotes: 112

Baurzhan Kozhaev
Baurzhan Kozhaev

Reputation: 123

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

Upvotes: 8

cockypup
cockypup

Reputation: 1043

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.

Upvotes: -1

GoZoner
GoZoner

Reputation: 70135

This should do it using built in functions.

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

Upvotes: 16

BlackJoker
BlackJoker

Reputation: 3191

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

Upvotes: 10

Related Questions