Java 将列表中的每个值指定给二维数组
我真的不知道如何定义列表中单个元素对2D数组的赋值。例如: 我有一个字符串列表,它包含:Java 将列表中的每个值指定给二维数组,java,arrays,list,Java,Arrays,List,我真的不知道如何定义列表中单个元素对2D数组的赋值。例如: 我有一个字符串列表,它包含: list.get(0) = "1 2 3" list.get(1) = "1 4 2" 我希望将每个元素按如下方式分配给int[]: tab[0][0] = 1; tab[0][1] = 2; tab[0][2] = 3; tab[1][0] = 1; tab[1][1] = 4; tab[1][2] = 2; 我编写了这样的代码: Scanner scan =
list.get(0) = "1 2 3"
list.get(1) = "1 4 2"
我希望将每个元素按如下方式分配给int[]:
tab[0][0] = 1;
tab[0][1] = 2;
tab[0][2] = 3;
tab[1][0] = 1;
tab[1][1] = 4;
tab[1][2] = 2;
我编写了这样的代码:
Scanner scan = new Scanner(System.in);
List<String> stringToMatrix = new ArrayList<>();
while (!stringToMatrix.contains("end")) {
stringToMatrix.add(scan.nextLine());
}
stringToMatrix.remove(stringToMatrix.size() - 1);
//-size of matrix
int col = stringToMatrix.get(0).length() - stringToMatrix.get(0).split(" ").length + 1;
int rows = stringToMatrix.size();
int[][] bigMatrix = new int[rows+2][col+2]; //rows and cols +2 because I want to insert values from the list into the middle of the table.
int outerIndex = 1;
for (String line: stringToMatrix) {
String[] stringArray = line.split(" ");
int innerIndex = 1;
for (String str: stringArray) {
int number = Integer.parseInt(str);
bigMatrix[outerIndex][innerIndex++] = number;
}
outerIndex++;
}
for (int[] x: bigMatrix) {
System.out.print(Arrays.toString(x));
}
结果:
[0, 0, 0, 0, 0][0, 1, 2, 3, 0][0, 1, 2, 3, 0][0, 0, 0, 0, 0]
输入:
1 2 3
1 2 3
end
1 -2 53 -1
1 4 -4 24
end
结果
[0, 0, 0, 0, 0, 0, 0, 0, 0][0, 1, -2, 53, -1, 0, 0, 0, 0][0, 1, 4, -4, 24, 0, 0, 0, 0][0, 0, 0, 0, 0, 0, 0, 0, 0]
问题就在这里:
int col = stringToMatrix.get(0).length() - stringToMatrix.get(0).split(" ").length + 1;
下面是一个如何将其转换为2d int[]数组的小示例
List<String> myListIWantToConvert = new ArrayList<Stirng>();
myListIWantToConvert.add("1 2 3");
myListIWantToConvert.add("1 4 2");
int[][] myConvert = new int[][];
int i = 0;
for(String string : myListIWantToConvert) {
// create new int array
int[] arrayToPutIn = new int[];
// split the string by the space
String[] eachNumber = string.split(" ");
// loog through string array
for(int i2 = 0; i2 < eachNumber.length; i2++) {
// convert string to integer and put in the new integer array
arrayToPutIn[i2] = Integer.valueOf(eachNumber[i2]);
}
// finally add the new array
myConvert[i] = arrayToPutIn;
i++; // edit: ( sry forgot :D )
}
下面是一个如何将其转换为2d int[]数组的小示例
List<String> myListIWantToConvert = new ArrayList<Stirng>();
myListIWantToConvert.add("1 2 3");
myListIWantToConvert.add("1 4 2");
int[][] myConvert = new int[][];
int i = 0;
for(String string : myListIWantToConvert) {
// create new int array
int[] arrayToPutIn = new int[];
// split the string by the space
String[] eachNumber = string.split(" ");
// loog through string array
for(int i2 = 0; i2 < eachNumber.length; i2++) {
// convert string to integer and put in the new integer array
arrayToPutIn[i2] = Integer.valueOf(eachNumber[i2]);
}
// finally add the new array
myConvert[i] = arrayToPutIn;
i++; // edit: ( sry forgot :D )
}
带有另一个嵌套循环的for循环应执行以下操作:
List<String> list = ... // input list
int[][] tab = new int[2][3]; // target array
int outerIndex = 0; // X-index of tab[X][Y]
for (String line: list) { // for each line...
String[] stringArray = line.split(" "); // ... split by a space
int innerIndex = 0; // ... Y-index of tab[X][Y]
for (String str: stringArray) { // ... for each item in a line
int number = Integer.parseInt(str); // ...... parse to an int
tab[outerIndex][innerIndex++] = number; // ...... add to array tab[X][Y]
} and increase the Y
outerIndex++; // ... increase the X
}
带有另一个嵌套循环的for循环应执行以下操作:
List<String> list = ... // input list
int[][] tab = new int[2][3]; // target array
int outerIndex = 0; // X-index of tab[X][Y]
for (String line: list) { // for each line...
String[] stringArray = line.split(" "); // ... split by a space
int innerIndex = 0; // ... Y-index of tab[X][Y]
for (String str: stringArray) { // ... for each item in a line
int number = Integer.parseInt(str); // ...... parse to an int
tab[outerIndex][innerIndex++] = number; // ...... add to array tab[X][Y]
} and increase the Y
outerIndex++; // ... increase the X
}
样品溶液:
输出:
1.
2.
3.
1.
4.
2样品溶液:
输出:
1.
2.
3.
1.
4.
2注意整数数组的大小。如果列表中包含不同数量的元素,则应考虑以下内容:
import java.util.*;
class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1 2 3");
list.add("4 5 6 7");
int[][] tab = new int[list.size()][];
for(int i = 0; i < list.size(); i++) {
String[] value = list.get(i).split(" ");
tab[i] = new int[value.length];
for(int j = 0; j < value.length; j++) {
tab[i][j] = Integer.parseInt(value[j]);
}
System.out.println( Arrays.toString(tab[i]));
}
}
}
注意int数组的大小。如果列表中包含不同数量的元素,则应考虑以下内容:
import java.util.*;
class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1 2 3");
list.add("4 5 6 7");
int[][] tab = new int[list.size()][];
for(int i = 0; i < list.size(); i++) {
String[] value = list.get(i).split(" ");
tab[i] = new int[value.length];
for(int j = 0; j < value.length; j++) {
tab[i][j] = Integer.parseInt(value[j]);
}
System.out.println( Arrays.toString(tab[i]));
}
}
}
您必须注意标注定义 使用二维数组时,它是一个包含数组的数组,2x3维数组如下所示:
[[x,y,z]]
[[a,b,c]]
["1 2 3"]["1 4 2"]
在更有用的可视化中:
[] item 1 of 2 of first dimension
[x,y,z] the 3 items of the second dimension
[] item 1 of 2 of first dimension
[a,b,c] the 3 items of the second dimension
现在,假设列表是一个一维数组,如果您的示例是这样的话:
[[x,y,z]]
[[a,b,c]]
["1 2 3"]["1 4 2"]
现在让我们将字符串更改为字符数组
[['1','2','3']]
[['1','4','2']]
或
你能看到相似之处吗
现在要得到你想要的,我们需要删除空格,并将它们转换成整数
所以你的算法是:
Create a new array of the desired dimensions (2x3) in your
define two variables to hole the value for line and column index with 0 value
loop over the list of strings:
transform this line in a array of strings, splinting it by ' '
loop over the splinted values and convert them to int
add your converted value in array[line][column]
increase column by 1
increase line by 1
set column value to 0
试试看,玩得开心:D您必须注意维度定义 使用二维数组时,它是一个包含数组的数组,2x3维数组如下所示:
[[x,y,z]]
[[a,b,c]]
["1 2 3"]["1 4 2"]
在更有用的可视化中:
[] item 1 of 2 of first dimension
[x,y,z] the 3 items of the second dimension
[] item 1 of 2 of first dimension
[a,b,c] the 3 items of the second dimension
现在,假设列表是一个一维数组,如果您的示例是这样的话:
[[x,y,z]]
[[a,b,c]]
["1 2 3"]["1 4 2"]
现在让我们将字符串更改为字符数组
[['1','2','3']]
[['1','4','2']]
或
你能看到相似之处吗
现在要得到你想要的,我们需要删除空格,并将它们转换成整数
所以你的算法是:
Create a new array of the desired dimensions (2x3) in your
define two variables to hole the value for line and column index with 0 value
loop over the list of strings:
transform this line in a array of strings, splinting it by ' '
loop over the splinted values and convert them to int
add your converted value in array[line][column]
increase column by 1
increase line by 1
set column value to 0
试试看,玩得开心:D将列表中的每个元素拆分到任何有空间的位置,并将拆分的部分存储在另一个1D数组中。例如num=[1,2,3,1,4,2]。然后使用嵌套循环访问二维数组中的索引,并将一维数组中的元素插入其中。在找到空间的位置拆分列表中的每个元素,并将拆分的部分存储在另一个一维数组中。例如num=[1,2,3,1,4,2]。然后使用嵌套循环访问二维数组中的索引,并将一维数组中的元素插入其中。谢谢,这超出了我的预期。但是如果我有像-12、2356这样的数字,您没有指定!你必须给我们所有的信息才能得到一个有效的答案。但是,您是否尝试过该代码?我打赌只要被``空格分割的部分保持不变且为整数,它就会工作。。。可以处理负数。你是对的,不幸的是,在代码通过测试之前我没有预见到这一点…代码经过测试,负数和num>9会导致奇怪的结果谢谢你的时间@尼古拉斯谢谢你,这比我预期的要多。但是如果我有像-12356这样的数字?你没有指定!你必须给我们所有的信息才能得到一个有效的答案。但是,您是否尝试过该代码?我打赌只要被``空格分割的部分保持不变且为整数,它就会工作。。。可以处理负数。你是对的,不幸的是,在代码通过测试之前我没有预见到这一点…代码经过测试,负数和num>9会导致奇怪的结果谢谢你的时间@尼古拉斯