如何在数组java中找到最大的三个数字?

时间:2012-09-10 12:53:37

标签: java arrays

我有一系列数字,我需要最大的三个数字和相应的索引值。我有一个像这样的数组:

int [] value = new int[5];
value[0] = 8;
value[1] = 3;
value[2] = 5;
value[3] = 2;
value[4] = 7;

如何找到最大的数字及其索引值?

5 个答案:

答案 0 :(得分:8)

我怀疑这是作业,所以我会提供一些帮助,但不是一个完整的解决方案。

您需要最大的三个数字,以及它们的索引值吗?

好吧,走过阵列,跟踪到目前为止找到的最高三个数字。还要跟踪他们的索引号。

你可以从最大数量及其索引开始。这应该很容易。 它需要两个变量,例如BiggestNumberindexOfBiggestNumber。首先找到最大的数字(平凡),然后添加一些代码来记住它的索引。

一旦你有了这个,你可以添加一些代码来跟踪第二大数字和它的索引。

之后,你会为第三大数字做同样的事情。

答案 1 :(得分:3)

我已经为你完成了,这很有效。

这里有完整的代码:

import java.util.Arrays;

class tester{
    public static void main(String[] args){
        int [] value = new int[5];
        value[0] = 8;
        value[1] = 3;
        value[2] = 5;
        value[3] = 2;
        value[4] = 7;

        int size=value.length;

        int[] temp=(int[])value.clone();

        Arrays.sort(temp);

        for(int i=0;i<3;i++)
        {

           System.out.println("value: "+temp[size-(i+1)]+" index "+getIndex(value,temp[size-(i+1)]));
        }

    }

   static int getIndex(int[] value,int v){

       int temp=0;
        for(int i=0;i<value.length;i++)
        {
            if(value[i]==v)
            {
                temp=i;
                break;
            }
        }

        return temp;

    }

}

答案 2 :(得分:1)

无需遍历数组并跟踪这么多变量,您可以利用已经实现的方法,如下所示。

我建议使用Map.Entry<key,value > (where key=index and value=number)列表,然后使用重写的Comparator方法实现compare接口(以对值进行排序)。一旦实现它,只需对列表进行排序。

public static void main(String[] args) {

    int [] value={5,3,12,12,7};
    Map <Integer, Integer> map = new HashMap<Integer, Integer>();
    for(int k=0;k<value.length;k++)
        map.put(k, value[k]);

    List<Map.Entry<Integer, Integer>> list =
            new LinkedList<Map.Entry<Integer,Integer>>(map.entrySet());

    Collections.sort(list, new Comparator<Map.Entry<Integer,Integer>>() {
        @Override
        public int compare(
                Entry<Integer, Integer> e1,
                Entry<Integer, Integer> e2) {
            return e2.getValue().compareTo(e1.getValue());
        }
    });

   for(Entry<Integer, Integer> lValue:list)
        System.out.println("value = "+lValue.getValue() +" , Index = "+lValue.getKey());
}

<强>结果:

 value = 12 , Index = 2
 value = 12 , Index = 3
 value = 7 , Index = 4
 value = 5 , Index = 0
 value = 3 , Index = 1

通过这种方法,您可以获得带有索引的前N个最大数字。

答案 3 :(得分:0)

要获得三个最大的,基本上,你排序,并选择最后三个条目。

获取他们的索引需要更多的工作,但绝对可行。只需将数字及其索引捆绑在一个Comparable中,其compareTo函数只关心数字。排序,获取最后三项,现在你有每个数字它的索引。

class IntWithIndex implements Comparable<IntWithIndex> {
    public int number, index;
    public IntWithIndex(number, index) { this.number = number; this.index = index; }
    public int compareTo(IntWithIndex other) { return number - other.number; }
}

...
IntWithIndex iwi[] = new IntWithIndex[yourNumbers.length];
for (int i = 0; i < yourNumbers.length; ++i) {
    iwi[i] = new IntWithIndex(yourNumbers[i], i);
}
Arrays.sort(iwi);

int largest      = iwi[iwi.length - 1].number;
int largestIndex = iwi[iwi.length - 1].index;
// and so on

答案 4 :(得分:-1)

按降序对数组进行排序,并显示前3个元素。

相关问题