简体   繁体   中英

How to find the biggest three numbers in an array java?

I have an array of numbers and I need the biggest of three number with respective index value. I have an array like this:

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

How to find the largest numbers and their index values?

I suspsect this is homework, so I'm going to give some help, but not a full solution.

You need the biggest three numbers, as well as their index values?

Well, walk over the array, keeping track of the highest three numbers you have found so far. Also keep track of their index numbers.

You could start by doing this for only the biggest number and its index. That should be easy. It takes two variables, eg BiggestNumber and indexOfBiggestNumber . Start with finding the biggest number (trivial), then add some code to remember it's index.

Once you have that, you can add some more code to keep track of the second biggest number and it's index as well.

After that, you do the same for the third biggest number.

I have done it for you, and this works.

here goes the complete code:

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;

    }

}

No need to traverse through array and keep tracking of so many variables , you can take advantage of already implemented methods like below.

I would suggest to use a List of Map.Entry<key,value > (where key=index and value=number) and then implement Comparator interface with overridden compare method (to sort on values). Once you have implemented it just sort the list .

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

Results:

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

By this approach you can get top N largest numbers with their index.

To get the three biggest, basically, you sort, and pick the last three entries.

Getting their indexes takes a little more work, but is definitely doable. Simply bundle the number and its index together in a Comparable whose compareTo function only cares about the number. Sort, get the last three items, and now you have each number and its index.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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