在Java中从字符串构建锯齿状数组
我必须编码“从在Java中从字符串构建锯齿状数组,java,arrays,string,matrix,Java,Arrays,String,Matrix,我必须编码“从字符串创建矩阵”: * The string if formatted as follow: * - Each row of the matrix is separated by a newline * character \n * - Each element of the rows are separated by a space * For example, the String "0 1\n2 3" represent the *
字符串创建矩阵”:
* The string if formatted as follow:
* - Each row of the matrix is separated by a newline
* character \n
* - Each element of the rows are separated by a space
* For example, the String "0 1\n2 3" represent the
* matrix:
[0 1]
[2 3]
在练习中,矩阵可以是一个“锯齿阵列”,下面是我的代码:
int[][] tab = null
int compteur = 0
int position = 0
for (int i = 0; i < s.length(); i++) {
char l = s.charAt(i);
String p = String.valueOf(l);
if (Character.isDigit(s.charAt(i))) {
tab[compteur][position] = s.charAt(i);
position++;
else if(p.equals("/n")) {
position = 0;
compteur ++;
}
}
return tab
int[]tab=null
整数编译器=0
int位置=0
对于(int i=0;i
以下是代码:
public static int[][] method(String s) {
String[] x = s.split("\n");
int[][] out = new int[x.length][x[0].split(" ").length];
for (int i = 0; i < x.length; i++) {
String[] y = x[i].split(" ");
for (int j = 0; j < y.length; j++) {
out[i][j] = Integer.parseInt(y[j]);
}
}
return out;
}
输出:
上面的代码适用于平方矩阵,但这里的代码适用于任何类型的矩阵!:
public static int[][] method(String s) {
String[] x = s.split("\n");
int max = Integer.MIN_VALUE;
for (String string : x) {
int l = string.split(" ").length;
max = l > max ? l : max;
}
int[][] out = new int[x.length][max];
for (int i = 0; i < x.length; i++) {
String[] y = x[i].split(" ");
for (int j = 0; j < y.length; j++) {
out[i][j] = Integer.parseInt(y[j]);
}
}
return out;
}
输出:
@Lionel Ding的解决方案当然会起作用,但值得一提的是,使用streams可能会更优雅。但是,逻辑将保持不变-您按\n
字符分割字符串,然后按“”字符分割每一行,将单个字符串解析为int
s,并将结果收集到nint[][]
:
private static int[][] toMatrix(String s) {
return Arrays.stream(s.split("\n"))
.map(line -> Arrays.stream(line.split(" "))
.mapToInt(Integer::parseInt).toArray())
.toArray(int[][]::new);
}
数组在java中的大小是固定的。您可以增量创建更大的数组,在填充之前在第一次传递中确定维度,或者使用列表。请不要链接到png,而是将代码作为文本发布。您已经在询问矩阵的短语周围使用了引号。我们是否假设矩阵是真正的矩形(每行中的列数相同)…或者它可能是一个真正的“锯齿数组”“?这将是一个数组数组,其中每行中的元素数量可能不同。你好,Lionel和Mureinik,谢谢你们的回答。我有一个问题要问你们两个:Lionel,我读了你们的答案,可能我不懂一点,或者你们的代码不适用于每一个字符串。我是说,当你们说“int[][]out=new int[x.length][x[0].split(“”.length]”它只适用于方阵?如果我的字符串是“12\n 6 3 9”怎么办对于Mureinik:我不懂流,你有介绍这个的视频或API吗?我是初学者,谢谢!!!谢谢你的回答,但是Lionel我读了你的回答,可能我不懂一点,或者你的代码不适用于每个字符串。我是说当你说“int[][]out=new int[x.length][x[0]。拆分(“”.length]”它只适用于方阵?如果我的字符串是“12\n 6 3 9”怎么办?你是对的,我在我原来的帖子中添加了一个“非方阵”部分!
public static int[][] method(String s) {
String[] x = s.split("\n");
int max = Integer.MIN_VALUE;
for (String string : x) {
int l = string.split(" ").length;
max = l > max ? l : max;
}
int[][] out = new int[x.length][max];
for (int i = 0; i < x.length; i++) {
String[] y = x[i].split(" ");
for (int j = 0; j < y.length; j++) {
out[i][j] = Integer.parseInt(y[j]);
}
}
return out;
}
String s = "0 1\n2 3\n4 5 6\n1"
int[][] array = [0 1 0],[2 3 0],[4 5 6],[1 0 0]
private static int[][] toMatrix(String s) {
return Arrays.stream(s.split("\n"))
.map(line -> Arrays.stream(line.split(" "))
.mapToInt(Integer::parseInt).toArray())
.toArray(int[][]::new);
}