英文:
How to store text file into 2d int array
问题
我有两个文件的格式如下,其中第一个整数需要存储在某个地方,而第二行及其以下内容将被存储在一个二维数组中。第一个数字,例如这里的 4,将指示数组的大小,如:int [][] array = new int [2*4][4]; 然而,4 不应是数组的一部分,应该被丢弃。文件路径将通过命令行提供给程序。我还没有找到可以执行此功能的任何代码。
4
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
英文:
I have two files formatted as follows below where the first int needs to be stored somewhere and the second line and down are going to be stored into a 2d array. The first number, in this case, 4, will indicate the size as int [][] array = new int [2*4][4]; The 4, however, should not be part of the array and should be discarded. The file paths will be provided to the program via the command line. I haven't been able to find any could that could perform this function.
4
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
答案1
得分: 1
我使用了Java 7的API将文件读取到列表中。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class SOTest {
public static void main(String[] args) throws IOException {
int[][] arr = null;
String path = args[0];
List<String> lines = Files.readAllLines(Paths.get(path));
for (int i=0; i<lines.size() ; i++) {
String[] aStr = lines.get(i).split("\\s+");
if(aStr.length==1) {
int size = Integer.valueOf(aStr[0]);
System.out.println("数组大小:"+size);
arr = new int[size][size];
populateArr(arr, lines.subList(i+1, i+size+1));
print(arr);
i = i+size;
}
}
}
public static void populateArr(int[][] arr, List<String> list) {
for(int i=0; i<list.size(); i++) {
String[] values = list.get(i).split("\\s+");
for(int j=0; j<values.length; j++) {
arr[i][j] = Integer.parseInt(values[j]);
}
}
}
private static void print(int[][] arr) {
System.out.println("***********打印***********");
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
输入(文件位置)
文件内容如下:
4
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
3
1 2 3
4 5 6
7 8 9
Java程序的输出:
数组大小:4
***********打印***********
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
数组大小:3
***********打印***********
1 2 3
4 5 6
7 8 9
英文:
I have used java 7 API to read file into a list.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class SOTest {
public static void main(String[] args) throws IOException {
int[][] arr = null;
String path = args[0];
List<String> lines = Files.readAllLines(Paths.get(path));
for (int i=0; i<lines.size() ; i++) {
String[] aStr = lines.get(i).split("\\s+");
if(aStr.length==1) {
int size = Integer.valueOf(aStr[0]);
System.out.println("Size of array "+size);
arr = new int[size][size];
populateArr(arr, lines.subList(i+1, i+size+1));
print(arr);
i = i+size;
}
}
}
public static void populateArr(int[][] arr, List<String> list) {
for(int i=0; i<list.size(); i++) {
String[] values = list.get(i).split("\\s+");
for(int j=0; j<values.length; j++) {
arr[i][j] = Integer.parseInt(values[j]);
}
}
}
private static void print(int[][] arr) {
System.out.println("***********PRINT***********");
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Input (File location)
file content is as below
4
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
3
1 2 3
4 5 6
7 8 9
output from java program
Size of array 4
***********PRINT***********
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
Size of array 3
***********PRINT***********
1 2 3
4 5 6
7 8 9
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论