简体   繁体   English

查找数组中整数的最小值(Java)

[英]Finding the minimum value of int numbers in an array (Java)

I'm trying to find the minimum value of numbers in an array but it doesn't always work out properly.我试图找到数组中数字的最小值,但它并不总是能正常工作。 This is the code I wrote:这是我写的代码:

        for (int i=0; i < arr.length; i++ ) {
        min = arr[i];
        for (j=0; j < arr.length; j++) {
        if (arr[j] < arr[0]) {
            min = arr[j];
         }
        }
    }   

Can someone correct me please?有人可以纠正我吗?

There's no need for the outer loop, it only runs once and you don't use i anyway.不需要外循环,它只运行一次,无论如何你都不使用i why do you have it?你为什么有它?

For the inner loop, you need to compare against the minimum value.对于内部循环,您需要与最小值进行比较。 Right now you are comparing it against the first element in the array, which is not necessarily the minimum value.现在您正在将它与数组中的第一个元素进行比较,这不一定是最小值。

min = arr[0];
for (j=0; j < arr.length; j++) {
    if (arr[j] < min) {  //<---fix is here
        min = arr[j];
    }
}

Also you could start the loop at 1, since you don't need to compare arr[0] against itself (it was just assigned to min )您也可以从 1 开始循环,因为您不需要将arr[0]与自身进行比较(它只是分配给min

You are checking the first element in each iteration, you basically need to check minimum value您正在检查每次迭代中的第一个元素,您基本上需要检查最小值

if (arr[j] < min) {
  min = arr[j];
}
int min = arr[0];
for(int num : arr) {
    if (num < min){
        min = num;
    }
}

min now contains minimum value. min 现在包含最小值。

If arr is an array of non-primitive numerics, I'd recommend如果arr是非原始数字数组,我建议

java.util.Collections.min(java.util.Arrays.asList(arr));

as that will be simpler to maintain.因为这样维护起来会更简单。 There's some justification in reverting to hand-coding if you need to pull the minimum and maximum out at the same time but I'd advise against hand-coding loops if there's a library function available.如果您需要同时提取最小值和最大值,则有一些理由可以恢复手动编码,但如果有可用的库函数,我建议不要使用手动编码循环。

In any case, you ought to check arr != null and the existence of a zeroth element.在任何情况下,您都应该检查arr != null和第零个元素的存在。

A way to do it would be using the java.util.Arrays class:一种方法是使用 java.util.Arrays 类:

Example:例子:

public class ArraySort {
public static void main(String[] args) {
    int[] array = {12, 4, 6, 1, 56, 21, 77};
    Arrays.sort(array);
    System.out.println(array[0]);
}
}

From the Java doc, Arrays.sort(int[]) sort the specified array into ascending numerical order.从 Java 文档中, Arrays.sort(int[]) 将指定的数组按数字升序排序。

So the output here prints 1.所以这里的输出打印 1。

One option is sort your array and get the first element:一种选择是对数组进行排序并获取第一个元素:

import java.util.Arrays;

...

int ints[] = {30,5,7,4,10};
Arrays.sort(ints);

int min = ints[0];
int max = ints[ints.length - 1];

Here is a general algorithm for doing this.这是执行此操作的通用算法。 You can write the code for it.您可以为它编写代码。

Store the first item in the array as the current minimum.将数组中的第一项存储为当前最小值。

Loop through the array, starting at the second item (index 1).循环遍历数组,从第二项(索引 1)开始。

For each iteration of the array, check if the current item is less than the minimum.对于数组的每次迭代,检查当前项是否小于最小值。 If it is, store it as the new minimum.如果是,则将其存储为新的最小值。

Once the loop ends, you have the minimum!一旦循环结束,你就有了最小值!

TRY this:尝试这个:

int min = arr[0];
    for(int j=1;j<arr.length;j++)
    {
            if(min>arr[j])
            {
                min= arr[j];
            }
    }
    System.out.println("min no is "+min);
      int min=0; 
        for (int i = 0; i < array.length; i++) {
        if (min > array[i]) {
        min = array[i];
        }
}

    System.out.println(min);

simple way to get MAX and MIN获得 MAX 和 MIN 的简单方法

To get MIN获得MIN

System.out.println(getMinValue(your array)); 

and for MAX对于MAX

System.out.println(getMaxValue(your array));

I am entering 5 values and storing them in the array and looping through them to find the min value but it prints 0 all the time.我正在输入 5 个值并将它们存储在数组中并循环遍历它们以找到最小值,但它始终打印 0。 What's wrong in this code?这段代码有什么问题? I used the same logic for the max value and has no problem.我对最大值使用了相同的逻辑并且没有问题。

public class FindMinValue {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int i;
        int NUM_VALUES = 5;
        int[] userVals = new int [NUM_VALUES];
        
        System.out.println("Enter " + NUM_VALUES + " values one at a time.");
        
        int minVal = userVals[0]; //min value so far
        
        for(i = 0; i < userVals.length; ++i) {
            userVals[i] = scnr.nextInt();
            if(userVals[i] < minVal) {
                minVal = userVals[i];
            }
        }
        System.out.println("Min Value is :" + minVal);
        

    }

}

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

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