2013-11-25 2 views
0

문자열의 1 차원 배열을 2 차원 배열로 변환하려고합니다.1D 문자열 배열을 2D 배열로 변환

배열 요소 수와 동일한 행 수입니다. 각 요소는 구 개 요소 자체로 분할 한 후에 행

에 있어야하므로

일차원 배열 요소 수있다.

그러나 출력이 예외에 추가 한 차원 배열 java.lang.ArrayIndexOutOfBoundsException 9

사람이 도와 줄 수는?

public class StringFragementation 

{ 

public static String [][] mymethod(String [] mystring) 
    { 

     int row = 0, col = 0; 

     String[][] india = new String[2][9]; 

     String mystringno2[]; 

     mystringno2 = mystring; 

     int i = 0; 

     int j = 0; 

     String x = "_"; 

     int i1; 

     String Nahda=""; 

     int l; 

     for(l=0;l<mystringno2.length;l++) 

     { 

    Nahda = Nahda.concat(mystringno2[l]); 

     } 

     System.out.println(Nahda); 

     i1 =0; 

    while (j <Nahda.length()) 

    { 

     i = Nahda.indexOf(x, i); 

     i1 = i + 1; 

     i1 = Nahda.indexOf(x, i1); 

    if (i1 >Nahda.length()) 

    { 

     break; 

     } 

     i++; 

    india[row][col] = Nahda.substring(i, i1); 

    System.out.print("Element " + row + " " + col + " " + india[row][col]+"\t"); 

    col++; 

    } 

    return india; 

    } 

public static void main(String[] args) 

{ 

String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"}; 

String [][] singapore = new String[s.length][9]; 

singapore = mymethod(s); 

for(int col=0,k=0;col <9;col++) 

    for(int row=0;row<s.length;row++) 

    { 
    System.out.print( singapore[row][col++]+"\t" ) ; 

    } 

    System.out.println("\n"); 

} 

} 

답변

1

문제는 indexOf() 반환 문자열이 발견되지 -1 경우이다. 이제 국가 이름이있는 문자열의 끝에 "_"이 없기 때문에 indexOf 함수는 -1을 반환합니다. 따라서 루프가 종료되지 않고 col이 9가되어 IndexOutOfBoundsException이됩니다.

내가 잘못했을 수도 있지만 끝 부분에 "_"을 추가하십시오.

또한 루프에서 row을 늘리지 않으므로 어레이의 치수가 하나만 반환됩니다. 당신은 18 개국이 9 열이 2 행에 그들을 배치 할 수 있도록

편집

이 좋아, 지금은, 그것을 얻을. 그럼 당신은 결코 row을 증가시키지 않을 것입니다.

이 시도 :

public class StringFragementation 

{ 

public static String [][] mymethod(String [] mystring) 
{ 

    int row = 0, col = 0; 

    String[][] india = new String[2][9]; 

    String mystringno2[]; 

    mystringno2 = mystring; 

    int i = 0; 

    int j = 0; 

    String x = "_"; 

    int i1; 

    String Nahda=""; 

    int l; 

    for(l=0;l<mystringno2.length;l++) 

    { 

     Nahda = Nahda.concat(mystringno2[l]); 

    } 

    System.out.println(Nahda); 

    i1 =0; 

    // set i before the loop 
    i = Nahda.indexOf(x, i); 

    while (j <Nahda.length()) { 

     i1 = Nahda.indexOf(x, i + 1); 

     if (i1 == -1) { 
      // No more "_" we're at the end but we still need to add the last country! 
      india[row][col] = Nahda.substring(i, Nahda.length() - 1); 
      break; 
     } 

     india[row][col] = Nahda.substring(i + 1, i1); 

     // set i to the "_" after the current country. 
     i = i1; 

     System.out.print("Element " + row + " " + col + " " + india[row][col].toString()+"\n"); 

     col++; 

     if (col >= 9) { 
      // we're at the end of the first row, go to second row 
      col = 0; 
      row++; 
     } 

    } 

    return india; 

} 

public static void main(String[] args) 

{ 

    String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"}; 

    String [][] singapore = new String[s.length][9]; 

    singapore = mymethod(s); 

    for(int col=0; col < 9;col++) 

     for(int row=0;row<s.length;row++) 

     { 
      System.out.print( singapore[row][col]+"\t" ) ; 

     } 

    System.out.println("\n"); 

} 

} 
+0

내 루프에서 줄을 늘리려고했지만 불행히도 doesn; t]] – JavaFan

+0

@ElhadiMamoun가 내 대답을 다시 확인하고 일부 코드를 추가했습니다. – Octoshape

+0

I 방금 시도해 봤는데 .. 작동합니다. 내 대답에 전체 수업을 게시 할 것입니다. – Octoshape

0

난 당신이 아니라 그것의 subSting을 복용보다 토큰의 문자열을 분할하는 것이 좋습니다. 이 유형의 질문에 C 스타일 솔루션이 더 많이 사용됩니다. 이건 어때?

public static String[][] mymethod(String[] input) { 
     String[][] result = new String[input.length][]; 
     for(int i = 0 ; i < input.length ; i++) { 
      List<String> temp = Arrays.asList(input[i].split("_")); 
      temp = temp.subList(1, temp.size()); 
      String[] row = new String[temp.size()]; 
      temp.toArray(row); 
      result[i] = row; 
     } 
     return result; 
    } 

희망이 있습니다.

+0

문제는 이미 문자열의 시작 부분에 _이 있습니다. 입력을 변경할 수 있는지 확실하지 않습니다. – Octoshape

+0

_로 나눌 경우 첫 번째 _는 처음 생성 된 토큰을 빈 문자열로 만듭니다. 위의 라인을 사용하여 제거됩니다 : temp = temp.subList (1, temp.size()); 그래서 괜찮을거야. – KKKCoder

+0

오, 하위 목록 :) 좋은 생각을 보지 못했어요. – Octoshape

1

여기 코드의 수정 버전입니다 : 당신이 (새로운 2 차원 배열로 전환 열 수를 계산하는 것을 잊었다

public class StringFragementation 

{ 

public static String [][] mymethod(String [] mystring) 
    { 

     int row = 0, col = 0; 

     String[][] india = new String[2][9]; 

     String mystringno2[]; 

     mystringno2 = mystring; 

     int i = 0; 

     int j = 0; 

     String x = "_"; 

     int i1; 

     String Nahda=""; 

     int l; 

     for(l=0;l<mystringno2.length;l++) 

     { 

    Nahda = Nahda.concat(mystringno2[l]); 

     } 

     System.out.println(Nahda); 

     i1 =0; 

    while (j <Nahda.length()) 

    { 

     i = Nahda.indexOf(x, i); 

     i1 = i + 1; 

     i1 = Nahda.indexOf(x, i1); 

    if (i1 <0) 

    { 

     break; 

     } 

     i++; 

    india[row][col] = Nahda.substring(i, i1); 

    System.out.print("Element " + row + " " + col + " " + india[row][col]+"\n"); 

    col++; 
     if (col > 8) { 
      row++; 
      col=0; 
     } 
    } 

    return india; 

    } 

public static void main(String[] args) 

{ 

String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"}; 

String [][] singapore = new String[s.length][9]; 

singapore = mymethod(s); 

for(int col=0,k=0;col <9;col++) 

    for(int row=0;row<s.length;row++) 

    { 
    System.out.print( singapore[row][col++]+"\t" ) ; 

    } 

    System.out.println("\n"); 

} 

} 

하고 8 증가 열 이상이고 경우 0 행을 다시, 또한 indexOf가 -1을 반환한다는 것을 잊어 버렸습니다. 그 점도 고쳐졌습니다.

관련 문제