【发布时间】:2018-03-31 00:38:21
【问题描述】:
我正在尝试使用可比较的接口对随机生成的数组进行排序,但收到错误消息:
不兼容的类型:int[] 无法转换为 Comparable[]
int [] list;
list = new int[n];
for(int i=0;i<n;i++){
list[i]=(int)(1+n*Math.random());
}
sortingoutsorts.bubble(list);
}
private static void swap(Object [] list, int x, int y)
{
Object temp=list[x];
list[x]=list[y];
list[y]=temp;
}
public static void bubble(Comparable [] list)
{
boolean done=false;
while(!done)
{
done=true;
for(int i=0; i+1<list.length; i++)
if(list[i].compareTo(list[i+1])>0)
{ swap(list,i,i+1); done=false; }
}
}
【问题讨论】:
-
将
int[]更改为Integer[]。 -
或者使用
<和>比较整数。
标签: java comparable compareto