## 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!

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

**How to find the minimum value in an ArrayList, along with the index ,** This java program find minimum and maximum value. // of an unsorted list of Integer by using Collection. import java.util.ArrayList;. If you want to find index of minimum or maximum element instead of value, you can use indexOf method of the ArrayList class. 1. int indexOf(Object o) This method returns the index of the specified element. If the element is not found in the ArrayList, it returns -1. 1.

This should do it using built in functions.

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

**Min and Max in a List in Java,** In this example we are finding out the maximum and minimum values from an int array. class MinMaxExample { public static void main(String args[]){ int. In order to compute minimum element of ArrayList with Java Collections, we use the Collections.min () method. The java.util.Collections.min () returns the minimum element of the given collection. All elements must be mutually comparable and implement the comparable interface. They shouldn’t throw a ClassCastException.

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

**Java,** Let's see how to find the index of the smallest number in an array in java, This program Java program to find index of max value in array java. Find maximum element of ArrayList with Java Collections Java 8 Object Oriented Programming Programming In order to compute maximum element of ArrayList with Java Collections, we use the Collections.max() method.

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

**Find the index of the smallest number in an array,** import java. util. Arrays; � public class MinMaxValues{ � public static void main ( String args[]){ � int numbers[]= {1,5,-9,12,-3,89, 18,23,4,-6}; � //Find minimum ( lowest)� To find maximum and minimum values in an array in Java you can use one of the following options-Iterate the array and look for the maximum and minimum values. See example. You can also write a recursive method to recursively go through the array to find maximum and minimum values in an array. See example.

**Three ways to find minimum and maximum values in a Java array of ,** Now, get the value of maximum and minimum of the range. We need to find the maximum and minimum of all numbers within this range. Create two variables ’max’ and ’min’ to hold the maximum and minimum values . Set values to both ’max’ and ’min’ as the first number of the list. Iterate through the list one by one.

These are very straightforward methods to get the maximum or minimum value of an array but there is a cleaner way to do this. Using Arrays.sort method to Find Maximum and Minimum Values in an Array int[] nums={6,-1,-2,-3,0,1,2,3,4}; Arrays.sort(nums); System.out.println("Minimum = " + nums[0]); System.out.println("Maximum = " + nums[nums.length-1]);

Convert a List of String to a comma separated String in Java; Convert an ArrayList of String to a String array in Java; Replace a character at a specific index in a String in Java; Java.lang.string.replace() method in Java; Print first letter of each word in a string using regex; Extracting each word from a String using Regex in Java

You need to take a value out of the list an compare it to all other values. If there is no smaller one, it's the smallest. If there is, you need do check the next one.

##### Comments

- 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
- 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?
`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
- And what about the index?