0

i have this arraylist and i want to put the data in array.but i have the problem to separate the data. ArrayList data_list:

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

note:each of letter is separate by space. i want to separate the name of the country to into separate letter such as germany becomes g e r m a n y i plan to convert the arraylist to 2d array.so the output become like this: 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

anyone can help me?

1
  • Is our original ArrayList an array list of String ? Commented Jul 25, 2011 at 6:51

4 Answers 4

1

Use toCharArray() method of String class.

Sign up to request clarification or add additional context in comments.

Comments

0

If your ArrayList looks like this:

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");

Then you can create your array of characters this way:

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));
}

The test output is

[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]

2 Comments

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
What code do you run actually? The above code alone runs without problems! (e.g. inside of a test method or static main method)
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]);

1 Comment

what is this?i dont understand
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(" ");

2 Comments

If you need the index in the loop, I usually avoid for-each loops. (Among other things, it limits the scope of the "temporary" variable.)
This solution doesn't handle the spaces between the letters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.