简体   繁体   English

Java数组到多维

[英]Java array to multi dimensional

是否可以将一个100个字符的数组转换为10 * 10的二维数组?

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

Iterate throughout your list of 100 chars and divide it amongst the 10*10, Modulus (%) will probably be very useful. 遍历整个100个字符的列表并将其除以10 * 10,模数(%)可能非常有用。

You could use 2 nested for loops to assign the chars of the array to the appropriate element. 您可以使用2个嵌套的for循环将数组的char分配给适当的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM