[英]How to convert the string into a fixed int 2D array(special case)?
Here is my string 100000000000000000000000000000000000000000000000000000000000这是我的字符串 10000000000000000000000000000000000000000000000000000000000
a string combined with 60 1/0.与 60 1/0 组合的字符串。
I want to put it into a int Array[6][10].我想把它放到一个 int Array[6][10] 中。 I tried to add "," between each row, but it failed我试图在每一行之间添加“,”,但失败了
String data = "1000000000,0000000000,0000000000,0000000000,0000000000,0000000000";
String[] rows = data.split(",");
String[][] matrix = new String[rows.length][];
int r = 0;
for (String row : rows) {
matrix[r++] = row.split("\\|");
}
System.out.print(matrix);
Please help and solve this problem, thank you!请帮忙解决这个问题,谢谢!
Here's a very straightforward solution that does not require splitting on regex
or inserting commas:这是一个非常简单的解决方案,不需要拆分regex
或插入逗号:
String input = "100000000000000000000000000000000000000000000000000000000000";
int[][] matrix = new int[6][10];
for (int i = 0; i < 6; i++)
for (int j = 0; j < 10; j++)
matrix[i][j] = Integer.parseInt(input.charAt(i * 10 + j) + "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.