【发布时间】:2020-10-24 11:36:24
【问题描述】:
我必须编写这个“从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]
在练习中,矩阵可以是“锯齿状数组”,这是我的代码:
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
【问题讨论】:
-
数组在java中是固定大小的。您可以逐步创建更大的数组,在填充或使用列表之前确定第一遍中的维度。请不要链接到 png,而是将代码作为文本发布。
-
您在请求矩阵的短语周围使用了引号。我们是否假设矩阵是真正的矩形(每行中的列数相同)......或者它可能是一个真正的“锯齿状数组”?那将是一个数组数组,其中每行中的元素数可能不同。