Java 对数组排序并获取最大的3个元素
我是初学者java程序员。 我有这个作业:写一个静态方法,方法中的参数是一个数组。该方法返回3个最大的酒精饮料数组元素 我的问题是:有没有我写的更短的解决方案?Java 对数组排序并获取最大的3个元素,java,arrays,sorting,Java,Arrays,Sorting,我是初学者java程序员。 我有这个作业:写一个静态方法,方法中的参数是一个数组。该方法返回3个最大的酒精饮料数组元素 我的问题是:有没有我写的更短的解决方案? public static void alcoholMax(AlcoholDrink[] t){ // sort the AlcoholDrink[] t Arrays.sort(t, new Comparator<AlcoholDrink>() { // here I try so
public static void alcoholMax(AlcoholDrink[] t){
// sort the AlcoholDrink[] t
Arrays.sort(t, new Comparator<AlcoholDrink>() {
// here I try sorting the elements by their alcohol value
@Override
public int compare(AlcoholDrink o1, AlcoholDrink o2) {
int at1 = (int)o1.getAlcohol(); // get the alcohol value
int at2 = (int)o2.getAlcohol(); // get the alcohol value
if(at1>at2)
return -1;
else if(at1<at2 )
return 1;
return 0;
}
});
// get the 3 largest element of the sorted array
double t0 = t[0].getAlcohol();
double t1 = t[1].getAlcohol();
double t2 = t[2].getAlcohol();
// check, 1st > 2nd > 3rd, if this not true, return with null reference
if(t0>t1 && t1>t2)
System.out.println(t[0] + "\n" + t[1] + "\n" + t[2]);
else
System.out.println("null");
}
public static void main(String[] args){
AlcoholDrink[] t = new AlcoholDrink [8];
// new AlcoholDrink( String Name, String Stripping, int price, double alcohol)
// these are hungarian wines :). If somebody curious for the languange
t[0] = new AlcoholDrink("Kék Portói", "0.75 l", 1200, 20.5);
t[1] = new AlcoholDrink("Kék Oportó", "0.75 l", 1100, 10.5);
t[2] = new AlcoholDrink("Tokaji Asszú", "0.75 l ", 1600, 14.5);
t[3] = new AlcoholDrink("Egri Bikavér", "0.75 l", 1500, 23.5);
t[4] = new AlcoholDrink("Egri Leányka", "0.75 l", 1100, 8.5);
t[5] = new AlcoholDrink("Egri Merlot", "0.75 l", 1700, 18.5);
t[6] = new AlcoholDrink("Egri Medina", "0.75 l", 900, 16.5);
t[7] = new AlcoholDrink("Törley Talisman", "0.75 l", 750, 4.5);
alcoholMax(t);
// It is always return with "null"
公共静态无效酒精最大值(酒精饮料[]t){
//把酒精饮料分类
sort(t,new Comparator(){
//在这里,我尝试按酒精值对元素进行分类
@凌驾
公共int比较(酒精饮料o1、酒精饮料o2){
int at1=(int)o1.getAlcohol();//获取酒精值
int at2=(int)o2.getAlcohol();//获取酒精值
如果(at1>at2)
返回-1;
否则,如果(在第1秒>第3秒,如果这不是真的,则返回空引用
如果(t0>t1&&t1>t2)
System.out.println(t[0]+“\n”+t[1]+“\n”+t[2]);
其他的
System.out.println(“空”);
}
公共静态void main(字符串[]args){
酒精饮料[]t=新的酒精饮料[8];
//新酒精饮料(字符串名称、字符串剥离、整数价格、双酒精)
//这些是匈牙利葡萄酒:)。如果有人对这种语言感兴趣的话
t[0]=新的酒精饮料(“KéK Portói”,“0.75升”,1200,20.5);
t[1]=新的酒精饮料(“KéK Oportó”、“0.75 l”、1100、10.5);
t[2]=新的酒精饮料(“Tokaji Asszú”,“0.75升”,1600,14.5);
t[3]=新的酒精饮料(“Egri Bikavér”,“0.75升”,1500,23.5);
t[4]=新的酒精饮料(“Egri Leányka”,“0.75升”,1100,8.5);
t[5]=新型酒精饮料(“Egri梅洛”、“0.75升”、1700、18.5升);
t[6]=新型酒精饮料(“Egri Medina”、“0.75升”、900、16.5升);
t[7]=新的酒精饮料(“törley Talisman”,“0.75升”,750,4.5);
酒精最大值(t);
//它总是以“null”返回
我相信您的数组正在按升序排序
在这种情况下,您需要获得排序后的最后3个元素,或者更改比较器。我相信您的数组是按升序排序的
在这种情况下,您希望获得排序后的最后3个元素,或者更改比较器。在比较器中,如果小于,则返回-1;如果大于,则返回1;如果小于,则返回-1;如果大于,则返回1方法返回一个double,那么,您不应该将其强制转换为
int
,这将导致精度损失。此外,您可以自动比较double,而不是自己进行比较,如下所示:
Arrays.sort(t, new Comparator<AlcoholDrink>() {
// here I try sorting the elements by their alcohol value
@Override
public int compare(AlcoholDrink o1, AlcoholDrink o2) {
return o1.getAlcohol().compareTo(o2.getAlcohol());
}
});
目前我无法测试代码,但您可能正在以升序方式对数组进行排序。如果getAlcohol()
方法返回一个double,则不应将其强制转换为int
,这将导致精度损失。此外,您可以自动比较double,而不是自己进行比较,如下所示:
Arrays.sort(t, new Comparator<AlcoholDrink>() {
// here I try sorting the elements by their alcohol value
@Override
public int compare(AlcoholDrink o1, AlcoholDrink o2) {
return o1.getAlcohol().compareTo(o2.getAlcohol());
}
});
First sort the array descending order and get the first three element.
package sortingelementinarray;
public class SortElement
{
public static void main(String args[])
{
int array[] = {1,6,4,7,2,3};
int temp;
for(int j = 0 ; j < array.length; j++)
{
for(int i = 0; i < array.length-1; i++)
{
if(array[j] > array[i])
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
for(int abc : array)
{
System.out.println(abc);
}
}
}
目前我无法测试代码,但您可能正在以升序方式对数组进行排序。首先按降序对数组排序,然后获取前三个元素。
First sort the array descending order and get the first three element.
package sortingelementinarray;
public class SortElement
{
public static void main(String args[])
{
int array[] = {1,6,4,7,2,3};
int temp;
for(int j = 0 ; j < array.length; j++)
{
for(int i = 0; i < array.length-1; i++)
{
if(array[j] > array[i])
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
for(int abc : array)
{
System.out.println(abc);
}
}
}
包装分类元件;
公共类分拣机
{
公共静态void main(字符串参数[])
{
int数组[]={1,6,4,7,2,3};
内部温度;
对于(int j=0;j数组[i])
{
温度=阵列[j];
数组[j]=数组[i];
数组[i]=温度;
}
}
}
for(int abc:array)
{
系统输出打印LN(abc);
}
}
}
首先按降序对数组排序,并获得前三个元素。
包装分类元件;
公共类分拣机
{
公共静态void main(字符串参数[])
{
int数组[]={1,6,4,7,2,3};
内部温度;
对于(int j=0;j数组[i])
{
温度=阵列[j];
数组[j]=数组[i];
数组[i]=温度;
}
}
}
for(int abc:array)
{
系统输出打印LN(abc);
}
}
}
好的,我知道…我必须更改返回值:)在if案例中:)好的,我知道…我必须更改返回值:)在if案例中:)是的,我在酒精类中实现了可比较接口,但教授说:我们必须只实现o1.name.compareto(o2.name)逻辑,并且不能使用o1.getAlcohol().compareto(o2.getAlcohol()));在这种情况下,您可以将排序逻辑移动到酒精类,然后调用.Sort(t)。这应该具有相同的效果。此外,如果这个答案解决了您的问题,请标记为:)是的,我在酒精类实现了可比接口,但教授说:我们必须只实现o1.name.compareto(o2.name)逻辑,并且不得使用o1.getAlcohol().compareTo(o2.getAlcohol());在这种情况下,您可以将排序逻辑移动到Alcohol类,然后调用.Sort(t)。这应该具有相同的效果。此外,如果此答案解决了您的问题,请将其标记为:)