public class Stack {
public static void main(String[] args) {
// Strings[]
// names={"news","ask","man","querty","lang","love","poppye","zebra","hello"};
int[] names = { 31, 5343, 8776, 90, 123, 33 };// shows me an error
Selection sec = new Selection();
sec.sort(names);
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}
}
class Selection {
int valNow;
public void sort(Comparable[] names) {
for (int i = 0; i < names.length; i++) {
boolean swapOn = false;
int min = -200;
int pointer = 0;
for (int j = i + 1; j < names.length; j++) {
if (less(names[i], names[j])) {
if (valNow > min) {
min = valNow;
pointer = j;
System.out.println("Pointer:" + pointer + " "
+ "Highest:" + min);
swapOn = true;
}
}
}
if (swapOn) {
swap(names, i, pointer);
}
}
}
public void swap(Comparable[] name, int x, int y) {
System.out.println("Gonna swap:" + name[x] + " and " + name[y]);
Comparable inter = name[x];
name[x] = name[y];
name[y] = inter;
}
public boolean less(Comparable one, Comparable two) {
boolean send = false;
valNow = one.compareTo(two);
System.out.println(valNow);
if (valNow > 0) {
send = true;
} else if (valNow == 0) {
send = true;
}
return send;
}
}
我能夠傳遞和排序字符串數組可比[]中選擇類sort()方法,但傳遞int數組進行排序()表示我錯誤可比[]是不適用於int [] !!我需要用相同的代碼對所有類型的數據進行排序!請幫助我!通整數數組可比在Java
'String'實現Comparable接口。 'int'不是一個Object,所以不是。 – starf 2014-12-05 14:46:07
使用Integer對象而不是基元int – MihaiC 2014-12-05 14:49:04