【发布时间】:2018-01-21 16:15:13
【问题描述】:
我正在尝试根据x 值对数组进行排序,以防x 值相同,根据y 对其进行排序。在对打印的值进行排序后,我得到一个异常,我通过覆盖 Comparator 接口的 compare 方法来调用 compare 方法。
class coder
{
int x,y,index;
}
public class CoderRating implements Comparator<coder>{
public int compare(coder A,coder B)
{
if(A.x==B.x)
{
if(A.y<B.y)
return -1;
else if(A.y>B.y)
return 1;
else
return 0;
}
else if(A.x<B.x)
return -1;
else
return 1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
coder []c=new coder[n];
for(int i=0;i<n;i++)
{
c[i]=new coder();
c[i].x=sc.nextInt();
c[i].y=sc.nextInt();
c[i].index=i;
}
Arrays.sort(c);
for(int i=0;i<n;i++)
{
System.out.println(c[i].x + " "+c[i].y );
}
}
}
【问题讨论】:
-
有什么问题?
-
有什么异常? a\d 你从哪里得到的?
-
你介意把
coder class改成Coder class吗???如果你这样做我会很高兴的! ;-)(类名以大写开头)谢谢
标签: java arrays sorting comparator