현재 선택 정렬 및 거품 정렬 코드로 어려움이 있습니다. 선택 정렬은 학생 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;
}
}
}
}
어떤 의견이라도 고맙겠습니다! 당신에게 오류 감사합니다 : 당신이 당신의 코드에서 line numbers
언급하지 않은 것처럼
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이 client.Client.SortArrayBySelection (Client.java:270) client.Client.main에서 \t (Client.java:232) 예외에에서 \t 스레드 "주"java.lang.NullPointerException \t at client.Client.BubbleSort (Client.java:288) \t at client.Client.main (Client.java:246) – mandok