我正在寻找建议,因为运行以下代码时出现错误:
public class Test9 {
public static void main(String[] args) {
Object [] myObjects = {
new Integer(12),
new String("foo"),
new Integer(5),
new Boolean(true)
};
Arrays.sort(myObjects);
for(int i=0; i<myObjects.length; i++) {
System.out.print(myObjects[i].toString());
//System.out.print(" ");
}
}
}
我得到的错误是:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at java.lang.Integer.compareTo(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at Practicequestions.Test9.main(Test9.java:18)