简体   繁体   English

"<i>How to find the minimum value in an ArrayList, along with the index number?<\/i>如何在 ArrayList 中找到最小值以及索引号?<\/b> <i>(Java)<\/i> (爪哇)<\/b>"

[英]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.我需要在 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.我的arraylist 包含几个浮点数,我正在想办法获得最小浮点数的索引号,这样我就可以在代码的其他地方使用该索引号。 I'm a beginner, so please don't hate me.我是初学者,所以请不要讨厌我。 Thanks!谢谢!

"

You can use Collections.min and List.indexOf :您可以使用Collections.minList.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;
}

This should do it using built in functions.这应该使用内置函数来完成。

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

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

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

1.Declare a arraylist with Floats. 1.用浮点数声明一个数组列表。

2.Collection.min() - finding the minimum element in the list. 2.Collection.min() - 查找列表中的最小元素。

3.List.indexOf() - finding the index of the minimum element. 3.List.indexOf() - 查找最小元素的索引。

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

} }

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 .假设您的数组名为myArray At the end of this code minIndex has the index of the smallest value.在此代码的末尾minIndex具有最小值的索引。

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) 算法或n 阶算法,这意味着如果数组中有n 个元素,那么在知道答案之前必须查看所有元素。 O(n) algorithms are the worst ones because they take a lot of time to solve the problem. O(n) 算法是最糟糕的算法,因为它们需要花费大量时间来解决问题。

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.话虽如此,除非你有成千上万个值的庞大数组,否则不要担心优化,因为 O(n) 算法和更快的算法之间的差异不会很明显。

Here's what I do.这就是我所做的。 I find the minimum first then after the minimum is found, it is removed from ArrayList.我先找到最小值,然后在找到最小值后,将其从 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM