Is it possible to take an array of say 100 chars and turn it into a 2d array of 10*10?
-
1No. A 3D array has 10*10*10 cells, making 1000 chars. You have only 100.MAK– MAK2010-10-20 20:08:30 +00:00Commented Oct 20, 2010 at 20:08
-
Are the numbers 100 and 10 x 10 meaningful or arbitrary? I mean do you want something that works for just that case or for any case? Your wording suggests that it is arbitrary to me.BigMac66– BigMac662010-10-20 20:26:26 +00:00Commented Oct 20, 2010 at 20:26
Add a comment
|
2 Answers
Here you go
char[] chars = ("01234567890123456789012345678901234567890123456789" +
"01234567890123456789012345678901234567890123456789")
.toCharArray();
char[][] char2D = new char[10][10];
for (int i = 0; i < 100; i++)
char2D[i / 10][i % 10] = chars[i];
Now the this code...
System.out.println(Arrays.deepToString(char2D).replaceAll("],","],\n"));
...prints the following
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]