2014-03-05 56 views
-2

我已經寫了這段代碼,它編譯得很好,但是當我嘗試運行它時,給了我一個錯誤,指出字符串索引超出範圍:11.有誰能告訴我什麼即時通訊做錯了?字符串索引超出範圍11錯誤java

import java.util。*; 類問題2 {

public static void main(String[] args) 
{ 

    String[] section1 = {"Curie, Marie", "Feynman, Richard", "Germain, Sophie", 
         "Turing, Alan"}; 
    String[] section2 = {"Bolt, Usain", "Graf, Steffi","Hamm, Mia",}; 
    String[] section3 = {"Bach, Johann Sebastian", "Beethoven, Ludwig van", 
         "Mozart, Wolfgang Amadeus", "Schumann, Clara"}; 
    String [] merged = mergeSortedArrays(section1,section2); 

    //String[] merged = mergeSortedArrays(mergeSortedArrays(section1, section2),section3); 
    for (int z =0; z < merged.length-4; z++) { 
    System.out.println(merged[z] + ", "); 
    } 


} 

// Do not change the method header 
public static String[] mergeSortedArrays(String[] a1, String[] a2) { //method for merging and ordering 


    String[] temp = new String[a1.length + a2.length]; // new array with size of the two input arrays 
    int x; // variable declared for a for loop and other purposes 
    for(x = 0; x< a1.length -1; x++){ //1st for loop to put in names from a1 to the new array 

     temp[x] = a1[x]; // code to store the strings from one array to the other 

    } 

    for(int y = 0; y <a2.length -1; y++) { //2nd for loop to put in names from a2 to the new array 

    temp[x] = a2[y];   // code to store the strings from one array to the other 
     x = x +1;    //increments x, to help incease the index of the new array 
    } 
    String[] tempor = new String[1]; 
    boolean finish = true; // boolean to help break the while loop used later 

    while(finish == true) { // while loop to check the names to make them in order 
     finish = false;  //makes the boolean false 
    for(int i = 0; i < temp.length-1 ; i ++) { // 3rd for loop. To check between two strings 
     String temporary1 = temp[i];   // string variable that stores a string from the new array 
     String temporary2 = temp[i+1];  // string variable that stores the next string from the same new array 
     int minimum = Math.min(temporary1.length(), temporary2.length()); 
     for(int j = 0; j < minimum -1; j++) { //4th for loop. To check between characters of the two strings 


     if((int)(temporary1.charAt(j)) == (int)(temporary2.charAt(j))) { //ascii value of same index characters of the two strings are compared. An int casting is done here. 
     //do nothing. If the two ascii values are same we iterate the for loop and compare the ascii values of the next two same index characters 
     //move to next character 
     } 


     else{ //if two asciis are not same, we compare to see which one is bigger/smaller. 


     if((int)temporary1.charAt(j) > (int)temporary2.charAt(j)) { //condition, if ascii of first char is greater, we swap the two strings 
      tempor[0] = temp[i + 1]; 
      temp[i + 1] = temp[i]; 
      temp[i] = tempor[0]; 
      finish = true; 
      j = j + 100; 
     } 

     else{ 
      j = j + 100; 
      //do nothing 
     } 

     } 


     } 

    } 

    } 



    return temp; 

}}

+2

代碼正在訪問長度小於12的數組的第12個元素(索引11)。*搜索*以獲取錯誤消息和*讀取其他人提出的問題,以便您可以得到一些建議。 – user2864740

+0

首先表達你想做什麼? – mok

回答

1

您正在訪問第12要素。請看下面

for(int i = 0; i < temp.length ; i ++) { 
    String temporary1 =""+ temp[i]; 
    String temporary2 = ""+temp[i+1]; // when i is at last element you are accessing something after that element which is invalid. 
    // everything else 

所以你必須將它循環到temp.length-1。在更多地方也是如此。嘗試糾正它。

+0

嘿,我解決了你指出的問題。現在它向我展示了另一個錯誤「空指針異常」。我在編碼方面很新穎,如果你能幫助我解決這個問題,那將會很棒。我編輯了我的代碼。 – user3382229

+0

你可以給出空指針異常和你得到的任何輸出的整個堆棧跟蹤。 –

+0

顯示java.lang.NullPointerException \t在Question2.mergeSortedArrays(Question2.java:48) \t在Question2.main(Question2.java:13) \t在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) \t在陽光下.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) \t在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) \t在java.lang.reflect.Method.invoke(Method.java:597) \t at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272) > – user3382229