當前選擇排序和氣泡排序代碼有困難。 選擇排序用於按升序排序學生ID,並使用氣泡排序按升序排序姓氏。該方案在選擇選擇10或11選擇排序和氣泡排序錯誤
我的數組被聲明如下編譯,但崩潰:
student[] list = new student[100]; //my array
這是我有選擇排序和冒泡排序的代碼。我正在使用一個數組與方法:
if (choice == 10) { // Dissplay the sorted array by student id
SortArrayBySelection(list);
System.out.println("Sorted studentid are:");
for (int i =0; i< studentNumber; i++)
{
System.out.println(list[i]);
}
}
if (choice == 11){ // Display the sorted array by family name
BubbleSort(list);
System.out.println("The sorted names are:");
for(int i = 0; i < studentNumber; i++)
{
System.out.println(list[i].Getfamilyname());
}
}
} while (choice != 1);
}
public static void SortArrayBySelection(student[] arrayToSort){ // Function to sort out the array on sutdentid
for(int i = 0; i < arrayToSort.length-1; ++i)
{
int minIndex = i;
int studentid3 = arrayToSort[i].Getstudentid();
int studentid2 = arrayToSort[minIndex].Getstudentid();
for(int j = i + 1; j <arrayToSort.length; ++j)
{
int studentid1 = arrayToSort[j].Getstudentid();
if(studentid1 < studentid2)
{
minIndex = j;
}
}
int temp = studentid3;
studentid3 = studentid2;
studentid2 = temp;
}
}
public static void BubbleSort(student[] arraySort){
String t;
for(int i = 0; i<arraySort.length; i++){
for(int j=0; j<arraySort.length-1;j++){
String str1 = arraySort[j].Getfamilyname();
String str2 = arraySort[j+1].Getfamilyname();
if(str1.compareTo(str2)<0){
t = str1;
str1 = str2;
str2 = t;
}
}
}
}
任何建議,將不勝感激!謝謝 錯誤:
Exception in thread "main" java.lang.NullPointerException
at client.Client.SortArrayBySelection(Client.java:270)
at client.Client.main(Client.java:232)
Exception in thread "main" java.lang.NullPointerException
at client.Client.BubbleSort(Client.java:288)
at client.Client.main(Client.java:246)
我想拋出一些異常,哪一個? – Rufi
你得到的錯誤是什麼,'程序編譯但選擇選擇10或11時崩潰,它不會拋出任何錯誤?發佈錯誤日誌? –
錯誤: 異常在線程 「主」 顯示java.lang.NullPointerException \t在client.Client.SortArrayBySelection(Client.java:270) \t在client.Client.main(Client.java:232)在 異常線程「main」java.lang.NullPointerException \t at client.Client.BubbleSort(Client.java:288) \t at client.Client.main(Client.java:246) – mandok