从文件中获取整数值到数组
Getting integer values to array from file
我正在尝试通过文件中的分隔逗号获取整数值并将它们放入二维数组中。
文件是这样的;
0,7,9,0,0,14
7,0,10,15,0,0
9,10,0,11,0,2
0,15,11,0,6,0
0,0,0,6,0,9
14,0,2,0,9,0
所以我希望第一行在 array[0][0 到 5] 中,其他行也一样,例如 array[2][1] 必须是 10。但我想不通二维数组的算法完全在这里。
public static void main(String[] args) throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
Scanner sc = new Scanner(reader);
int[][] array = new int[6][6];
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] str = line.split(",");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
array[i][j] = Integer.valueOf(str[i]);
}
}
}
}
您正在循环遍历每一行的每个值。您的一个索引需要是线,另一个需要是线内的位置。你只需要一个 for
循环;你应该增加另一个索引作为你的 while 循环的一部分。
public static void main(String[] args) throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
Scanner sc = new Scanner(reader);
int[][] array = new int[6][6];
int i=0;
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] str = line.split(",");
for (int j = 0; j < 6; j++) {
array[i][j] = Integer.valueOf(str[j]);
}
i++;
}
}
您还应该考虑到如果文件超过 6 行或超过 6 行,这将失败,因此这不是非常健壮的代码。读取完文件后,填充不同的(动态)数据类型并转换为数组可能会更好。至少,您应该检查您是否没有超出指数的预期范围。
你可以这样做。关键是Java里面没有真正的多维数组。只是单个 D 数组也可以容纳数组。
// allocate the "2D" array but no need to specify the rows.
int[][] array = new int[6][];
// starting row number
int rowNumber = 0;
while(sc.hasNextLine()){
String line = sc.nextLine();
System.out.println(line);
String[] str = line.split(",");
// here is where your code differs
// allocate a new row based on length of items just read
int[] row = new int[str.length];
// Now copy and convert to ints.
for (int i = 0; i < str.length; i++) {
row[i] = Integer.parseInt(str[i]);
}
// Then just assign the row to the row number.
// you can do this because a two dimentional array is just an
// one dimenional array of another one dimensional array.
array[rowNumber++] = row;
}
// print them out for verification.
// get each row (from the array of rows)
for (int[] row : array) {
System.out.println(Arrays.toString(row));
}
所以做了如下;
int [][] matrix = new int[2][];
matrix[0] = new int[]{1,2,3,4};
// rows can be different sizes.
matrix[1] = new int[]{1,2,3,4,5,6,7};
for (int i = 0; i <2; i++) {
int[] row = matrix[i];
for (int j = 0; j < row.length; j++) {
System.out.print(row[j] + " " );
}
System.out.println(); // next line
}
这是一种更现代的方法,使用 NIO and Streams:
int[][] read2dArrayFromFile(String path) throws IOException {
return Files.readAllLines(Paths.get(path))
.stream()
.map(line ->
Arrays.stream(
line.split("\D+") // split by anything that's not a digit
).mapToInt(Integer::parseInt).toArray()
).toArray(int[][]::new);
}
我正在尝试通过文件中的分隔逗号获取整数值并将它们放入二维数组中。 文件是这样的;
0,7,9,0,0,14
7,0,10,15,0,0
9,10,0,11,0,2
0,15,11,0,6,0
0,0,0,6,0,9
14,0,2,0,9,0
所以我希望第一行在 array[0][0 到 5] 中,其他行也一样,例如 array[2][1] 必须是 10。但我想不通二维数组的算法完全在这里。
public static void main(String[] args) throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
Scanner sc = new Scanner(reader);
int[][] array = new int[6][6];
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] str = line.split(",");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
array[i][j] = Integer.valueOf(str[i]);
}
}
}
}
您正在循环遍历每一行的每个值。您的一个索引需要是线,另一个需要是线内的位置。你只需要一个 for
循环;你应该增加另一个索引作为你的 while 循环的一部分。
public static void main(String[] args) throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
Scanner sc = new Scanner(reader);
int[][] array = new int[6][6];
int i=0;
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] str = line.split(",");
for (int j = 0; j < 6; j++) {
array[i][j] = Integer.valueOf(str[j]);
}
i++;
}
}
您还应该考虑到如果文件超过 6 行或超过 6 行,这将失败,因此这不是非常健壮的代码。读取完文件后,填充不同的(动态)数据类型并转换为数组可能会更好。至少,您应该检查您是否没有超出指数的预期范围。
你可以这样做。关键是Java里面没有真正的多维数组。只是单个 D 数组也可以容纳数组。
// allocate the "2D" array but no need to specify the rows.
int[][] array = new int[6][];
// starting row number
int rowNumber = 0;
while(sc.hasNextLine()){
String line = sc.nextLine();
System.out.println(line);
String[] str = line.split(",");
// here is where your code differs
// allocate a new row based on length of items just read
int[] row = new int[str.length];
// Now copy and convert to ints.
for (int i = 0; i < str.length; i++) {
row[i] = Integer.parseInt(str[i]);
}
// Then just assign the row to the row number.
// you can do this because a two dimentional array is just an
// one dimenional array of another one dimensional array.
array[rowNumber++] = row;
}
// print them out for verification.
// get each row (from the array of rows)
for (int[] row : array) {
System.out.println(Arrays.toString(row));
}
所以做了如下;
int [][] matrix = new int[2][];
matrix[0] = new int[]{1,2,3,4};
// rows can be different sizes.
matrix[1] = new int[]{1,2,3,4,5,6,7};
for (int i = 0; i <2; i++) {
int[] row = matrix[i];
for (int j = 0; j < row.length; j++) {
System.out.print(row[j] + " " );
}
System.out.println(); // next line
}
这是一种更现代的方法,使用 NIO and Streams:
int[][] read2dArrayFromFile(String path) throws IOException {
return Files.readAllLines(Paths.get(path))
.stream()
.map(line ->
Arrays.stream(
line.split("\D+") // split by anything that's not a digit
).mapToInt(Integer::parseInt).toArray()
).toArray(int[][]::new);
}