CONTEXTSortiert int [] von String konvertiert [] - ArrayIndexOutOfBoundsException
ich für eine Weile diesen Teil meines Programms zu beheben habe versucht, jetzt ohne viel Erfolg. Ich möchte im Wesentlichen eine String []
sortieren, wobei jedes Element im Format: name:number
(d. H. john:32
) ist.
PROGRESS
Bisher teilt mein Code jedes Element und fügt sie zu einem gleichwertigen int []
. Ich versuche dann, die Elemente in der int []
mit Auswahlsortierung zu vergleichen und die Elemente in der String []
auszutauschen.
PROBLEM
ich java.lang.ArrayIndexOutOfBoundsException für meine String []
bin immer, die scores
genannt wird. Warum ist das?
scores = sort(scores); //ArrayIndexOutOfBoundsException here
public static String [] sort(String [] A) {
//equivalent array containing only integer part of score[i]
int[] tempArray = new int[A.length];
//populate tempArray
for(int i = 0; i < A.length; i++) {
//acquire numerical part of element
//ArrayIndexOutOfBoundsException here********
int num = Integer.parseInt(A[i].split(":")[1]);
//add to array
tempArray[i] = num;
}
/* Selection Sort: descendinG */
//compare elements (integer) in tempArray
for(int i = 0; i < tempArray.length; i++){
int index = i;
//search for integers larger for above index
for(int j = i+1; j < tempArray.length; j++){
if(tempArray[j] > tempArray[index]){
index = j;
}}
//swap elements in scores-array (String)
String temp = A[index];
A[index] = A[i];
A[i] = temp;
}
return A;
}
Offensichtlich haben Sie eine Zeichenfolge ohne ' ":"'. – shmosel
Wooow ... woher weißt du das? –
Es war die einzige Möglichkeit. – shmosel