java - 如何在数组中找到最大的三个数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12352151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the biggest three numbers in an array java?
提问by RAMKI JHON
I have an array of numbers and I need the biggest of three numberwith 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?
如何找到最大的数字及其索引值?
回答by S.L. Barth - Reinstate Monica
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, e.g. BiggestNumber
and indexOfBiggestNumber
. Start with finding the biggest number (trivial), then add some code to remember it's index.
您可以从仅对最大数字及其索引执行此操作开始。那应该很容易。它需要两个变量,例如BiggestNumber
和indexOfBiggestNumber
。首先找到最大的数字(微不足道),然后添加一些代码来记住它的索引。
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.
之后,您对第三大数字执行相同操作。
回答by Md. Arafat Al Mahmud
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;
}
}
回答by amicngh
No needto 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 .
我建议使用 List ofMap.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());
}
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.
通过这种方法,您可以获得前 N 个最大数字及其索引。
回答by cHao
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 andits index.
获取它们的索引需要做更多的工作,但绝对是可行的。只需将数字及其索引捆绑在一个 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
回答by user1249655
Sort the array in descending order and show the first 3 element.
按降序对数组进行排序并显示前 3 个元素。