Java array to multi dimensional – Education Career Blog

Is it possible to take an array of say 100 chars and turn it into a 2d array of 10*10?

,

Here you go

char chars = ("01234567890123456789012345678901234567890123456789" + 
                "01234567890123456789012345678901234567890123456789")
                .toCharArray();

char char2D = new char1010;

for (int i = 0; i < 100; i++)
    char2Di / 10i % 10 = charsi;

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

,

Iterate throughout your list of 100 chars and divide it amongst the 10*10,
Modulus (%) will probably be very useful.

You could use 2 nested for loops to assign the chars of the array to the appropriate element.

Leave a Comment