2011-07-25 22 views
0

私はこのarraylistを持っています。配列にデータを入れたいのですが、私はデータを分離するのに問題があります。 のArrayList DATA_LIST:javaで配列する文字列

g e r m a n y 
a u s t r a l i a 
n e w z e a l a n d 
e n g l a n d 
c o s t a r i c a 
p h i l i p i n a 
m y a n m a r 
t h a i l a n d 

注:レターの各スペースによって分離されています。 私は国の名前を別の文字に分割したいと思っています ドイツはドイツ語になります arraylistを2d配列に変換する予定です。出力は次のようになります。 String [] [] country;

country[0][0]=g 
country[0][1]=e 
country[0][2]=r 
country[0][3]=m 
country[0][4]=a 
country[0][5]=n 
country[0][6]=y 

country[1][0]=a 
country[1][1]=u 
country[1][2]=s 
country[1][3]=t 
country[1][4]=r 
country[1][5]=a 
country[1][6]=l 
country[1][7]=i 
country[1][8]=a 

誰でも手伝えますか?

+0

オリジナルのArrayListはStringの配列リストですか? –

答えて

0

あなたのArrayListのは、このようになります場合:

List<String> countries = Arrays.asList("g e r m a n y", "a u s t r a l i a", "n e w z e a l a n d", 
    "e n g l a n d", "c o s t a r i c a", "p h i l i p i n a", "m y a n m a r", "t h a i l a n d"); 

その後、あなたはこのような文字のあなたの配列を作成することができます。

String[][] countryLetters = new String[countries.size()][]; 
for (int i = 0; i < countries.size(); i++) { 
    String country = countries.get(i); 
    countryLetters[i] = country.split(" "); 
} 
// test output 
for (String[] c : countryLetters) { 
    System.out.println(Arrays.toString(c)); 
} 

テスト出力が

[g, e, r, m, a, n, y] 
[a, u, s, t, r, a, l, i, a] 
[n, e, w, z, e, a, l, a, n, d] 
[e, n, g, l, a, n, d] 
[c, o, s, t, a, r, i, c, a] 
[p, h, i, l, i, p, i, n, a] 
[m, y, a, n, m, a, r] 
[t, h, a, i, l, a, n, d] 
です
+0

スレッド "main"の例外java.lang.ArrayIndexOutOfBoundsException:0 – Roubie

+0

これは今動作しますか? コードはここで実行されます。 – migu

+0

まだcan not run.with d同じエラー – Roubie

1

StringクラスのtoCharArray()メソッドを使用します。

0
ArrayList<String> orig = new ArrayList<String>(); 
orig.add("G e r m a n y"); 
orig.add("A u s t r a l i a"); 

String[][] newArray = new String[orig.size()][]; 
int i = 0; 
for(String s : orig) 
    newArray[i++] = s.split(" "); 
+0

ループ内のインデックスが必要*の場合は、通常for-eachループを避けます。 (とりわけ、 "一時的な"変数の範囲を制限します。) – aioobe

+0

この解決策では、文字間のスペースは処理されません。 – migu

0
String a = "germany"; 
    String b = "india"; 
    char[] ar = a.toCharArray(); 
    char[] br = b.toCharArray(); 
    char [][] td = new char[2][2]; 
    td[0] = ar; 
    td[1] = br; 
    System.out.println(td); 
    System.out.println(td[0][0]+""+td[0][1]+""+td[0][2]+""+td[0][3]+""+td[0][4]+""+td[0][5]+""+td[0][6]); 
+0

これは何ですか?わかりません – Roubie