为了方便各位大牛,我把代码粘过来:
public class GenericSort {
public static void main(String[] args){
Integer[] intArray = {new Integer(2), new Integer(4), new Integer(3)};
Double[] doubleArray = {new Double(3.4), new Double(1.3), new Double(-22.1)};
Character[] charArray = {new Character('a'), new Character('j'), new Character('r')};
String[] stringArray = {"Tom", "John", "Fred"};
sort(intArray);
sort(doubleArray);
sort(charArray);
sort(stringArray);
System.out.println("Sorted Integer objects: ");
printList(intArray);
System.out.println("Sorted Double objects: ");
printList(doubleArray);
System.out.println("Sorted Character objects: ");
printList(charArray);
System.out.println("Sorted String objects: ");
printList(stringArray);
}
public static void sort(Object[] list){
Object currentMax;
int currentMaxIndex;
currentMaxIndex = 0;
for(int i = list.length - 1; i >= 1; i--){
currentMax = list[i];
currentMax = i;
for(int j = i - 1; j >= 0; j--){
if(((Comparable)currentMax).compareTo(list[j]) < 0){
currentMax = list[j];
currentMaxIndex = j;
}
}
if(currentMaxIndex != i){
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
}
public static void printList(Object[] list){
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
}
public class GenericSort {
public static void main(String[] args){
Integer[] intArray = {new Integer(2), new Integer(4), new Integer(3)};
Double[] doubleArray = {new Double(3.4), new Double(1.3), new Double(-22.1)};
Character[] charArray = {new Character('a'), new Character('j'), new Character('r')};
String[] stringArray = {"Tom", "John", "Fred"};
sort(intArray);
sort(doubleArray);
sort(charArray);
sort(stringArray);
System.out.println("Sorted Integer objects: ");
printList(intArray);
System.out.println("Sorted Double objects: ");
printList(doubleArray);
System.out.println("Sorted Character objects: ");
printList(charArray);
System.out.println("Sorted String objects: ");
printList(stringArray);
}
public static void sort(Object[] list){
Object currentMax;
int currentMaxIndex;
currentMaxIndex = 0;
for(int i = list.length - 1; i >= 1; i--){
currentMax = list[i];
currentMax = i;
for(int j = i - 1; j >= 0; j--){
if(((Comparable)currentMax).compareTo(list[j]) < 0){
currentMax = list[j];
currentMaxIndex = j;
}
}
if(currentMaxIndex != i){
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
}
public static void printList(Object[] list){
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
}
sort(doubleArray);
sort(charArray);
sort(stringArray);这些地方用Arrays.sort();Arrays.sort(intArray);
呃。。sort是根据下面写的sort方法来的。不是Arrays类里的。
呃...不好意思,我是个新菜鸟,不太懂您说的这个问题。
我想知道,应该怎么改这个程序?为啥直接调用源里的compareTo方法不可以呢?
您说的的“Double 和 Integer 对象类型不同(不能进行强转)”,但是sort方法里只是Integer类里的自己比较,Double类里的自己比较。没有夸类进行比较。
呃...不好意思,我是个新菜鸟,不太懂您说的这个问题。
我想知道,应该怎么改这个程序?为啥直接调用源里的compareTo方法不可以呢?大哥,你这么客气,我也是菜鸟。可以用,我的意思是sort(doubleArray);还是给你截个图吧你看,一个是Double对象,一个是Integer对象,他们两个之间不能强转,所以会报这个异常
呃...不好意思,我是个新菜鸟,不太懂您说的这个问题。
我想知道,应该怎么改这个程序?为啥直接调用源里的compareTo方法不可以呢?大哥,你这么客气,我也是菜鸟。可以用,我的意思是sort(doubleArray);还是给你截个图吧你看,一个是Double对象,一个是Integer对象,他们两个之间不能强转,所以会报这个异常我大概明白你的意思了。是说第一个sort(intArray);执行完后,开始执行sort(doubleArray);时,currentMax是Integer类型,但是list[i]是Double类型。所以出现了错误,是这个意思么?
那sort方法的第一行Object currentMax;这个,不会对currentMax进行初始化清除是么?如果是这个原因的话,我该怎么改?
currentMaxIndex = i;看红色的字,是不是这里打错了