有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在获取元素号的同时找到数组中的最小数

我有个问题一直困扰着我。找到数组中的最小数很容易,但这是我的问题;当您在数组中找到最小的数字时,最小的数字将成为数组的第[0]个元素。 我想要的是,我想要找到最小的数字,以及它是哪个元素。像这样

a[0]   a[1]   a[2]   a[3]
 5      20     1      12

我想让我的代码写1 is the smallest number and it's a[2] element of array

我只想从那里得到2,这样我就可以在代码的其余部分使用它了。任何帮助都将不胜感激。谢谢你们

编辑:我以前试过这种方法

int[] dizi = {5 , 12, 20, 1};
    int gecici;


    for (int i=0; i<4; i++) {
        for (int y = 1; y<4; y++) {

            if (dizi[i] > dizi[y]) {

                gecici = dizi[i];
                dizi[i] = dizi[y];
                dizi[y] = gecici;

            }

        }

    }

共 (5) 个答案

  1. # 1 楼答案

    int[] arr = {3,66,22,44,55};
    int small=arr[0];
    int index=0;
    for(int i=0;i<arr.length;i++){
         if(arr[i]<small){
                small = arr[i];
                index = i;
         }
    }
    
  2. # 2 楼答案

    如果要查找从小到大的每个元素及其相应的索引,则可以执行以下操作:

    public class Test {
        public static class Pair implements Comparable<Pair> {
            int value;
            int index;
            public Pair(int _value, int _index) {
                this.value = _value;
                this.index = _index;
            }
            @Override
            public int compareTo(Pair that) {
                return Integer.valueOf(this.value).compareTo(Integer.valueOf(that.value));
            }
        }
    
        public static void main(String args[]) {
            int[] a =new int[]{5, 20, 1, 12};
            int n = a.length;
            Pair[] p = new Pair[n];
            for (int i = 0; i < n; ++i) p[i] = new Pair(a[i], i);
            Arrays.sort(p);
            for (int i = 0; i < n; ++i) {
                System.out.println(i + "th minimum is "+ p[i].value +" and is located at index "+ p[i].index);
            }
        }
    }
    

    上述方法的复杂性为时间复杂性O(n log n)。但是,如果您只需要知道最小值和它的索引,那么您可以轻松地在O(n)时间复杂度中检索它,如下所示:

       int[] a =new int[]{5, 20, 1, 12};
        int n = a.length;
        int minValue = Integer.MAX_VALUE, minIndex = 0;
        for (int i = 0; i < n; ++i) {
            if (minValue > a[i]) {
                minValue = a[i];
                minIndex = i;
            }
        }
        System.out.println("Minimum value is : "+ minValue+ " and it is located at index: "+ minIndex);
    
  3. # 3 楼答案

    int [] array = {0,1,2,3,4,5,6,7,8,9,10};
    int smallestNum=array[0];
    int smallestIndex=0;
    for(int i=1;i<array[i];i++){
       if(array[i] < smallestNum){ //if you want the last small number then use `<=` (if small number occur multiple times)
          smallestNum = array[i];
          smallestIndex=i;
       }
    }
    
  4. # 4 楼答案

    试试这个

    int[] a = {5, 20, 1, 12};
    IntStream.range(0, a.length)
        .mapToObj(i -> i)
        .min(Comparator.comparing(i -> a[i]))
        .ifPresent(i -> System.out.printf(
            "%d is the smallest number and it's a[%d] element of array%n", a[i], i));
    

    如果你的数组是双倍的,那么

    double[] a = {5, 20, 1, 12};
    IntStream.range(0, a.length)
        .mapToObj(i -> i)
        .min(Comparator.comparing(i -> a[i]))
            .ifPresent(i -> System.out.printf(
                "%f is the smallest number and it's a[%d] element of array%n", a[i], i));
    

    你可以用一种方法来做

    /**
     * Returns min index of array x.
     * (Returns -1 when length of array x is zero)
     */
    static int findMinIndex(int[] x) {
        return IntStream.range(0, x.length)
            .mapToObj(i -> i)
            .min(Comparator.comparing(i -> x[i]))
            .orElse(-1);
    }
    

    像这样打电话

    int[] a = {5, 20, 1, 12};
    int minIndex = findMinIndex(a);
    System.out.printf("%d is the smallest number and it's a[%d] element of arraay%n",
        a[minIndex], minIndex);
    
  5. # 5 楼答案

    在这种情况下,您可以利用IntStream.range

    IntStream.range(0, arr.length)
             .mapToObj(index -> new SimpleEntry<>(index, arr[index]))
             .min(Comparator.comparingInt(SimpleEntry::getValue));
    

    例如:

    int[] arr = new int[]{5,20 ,1 ,12};
    IntStream.range(0, arr.length)
             .mapToObj(index -> new SimpleEntry<>(index, arr[index]))
             .min(Comparator.comparingInt(SimpleEntry::getValue))
             .ifPresent(s -> System.out.println(s.getValue()+ 
                   " is the smallest number and it's index" + s.getKey() + "of array"));