英文:
Build a jagged array from a String in Java
问题
我需要编写这个“从字符串创建矩阵”的代码:
* 字符串的格式如下:
* - 矩阵的每一行由换行符 \n 分隔
* - 每行的元素由空格分隔
* 例如,字符串 "0 1\n2 3" 表示以下矩阵:
[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;
英文:
I have to code this "create a matrix from a String
:
* 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]
In the exercise the matrix can be a "Jagged Array" and here is my code :
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
答案1
得分: 1
@Lionel Ding的解决方案当然有效,但值得一提的是,使用流可能会更加优雅。然而,逻辑仍将保持相同 - 通过\n
字符拆分字符串,然后通过' '字符拆分每一行,将个别字符串解析为int
并将结果收集到int[][]
中:
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);
}
英文:
@Lionel Ding's solution will work, of course, but it's worth mentioning that using streams could arguably be more elegant. The logic would remain the same, however - you split the string by the \n
character, and then split every line by the ' ' character, parse the individual strings to int
s and collect the result to an int[][]
:
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);
}
答案2
得分: 0
这是代码部分的翻译:
第一部分代码:
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;
}
输入:
String s = "0 1\n2 3\n4 5"
输出:
int[][] array = [0 1],[2 3],[4 5]
第二部分代码:
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]
英文:
Here's the code :
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;
}
IN :
String s = "0 1\n2 3\n4 5"
OUT :
int[][] array = [0 1],[2 3],[4 5]
This above code works fine for square matrix, but here the code that works for any kind of matrix ! :
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;
}
IN :
String s = "0 1\n2 3\n4 5 6\n1"
OUT :
int[][] array = [0 1 0],[2 3 0],[4 5 6],[1 0 0]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论