如何将文本文件存储到二维整数数组中

huangapple go评论60阅读模式
英文:

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&lt;String&gt; lines = Files.readAllLines(Paths.get(path));
for (int i=0; i&lt;lines.size() ; i++) {
String[] aStr = lines.get(i).split(&quot;\\s+&quot;);
if(aStr.length==1) {	
int size = Integer.valueOf(aStr[0]);
System.out.println(&quot;Size of array &quot;+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&lt;String&gt; list) {
for(int i=0; i&lt;list.size(); i++) {
String[] values = list.get(i).split(&quot;\\s+&quot;);
for(int j=0; j&lt;values.length; j++) {
arr[i][j] = Integer.parseInt(values[j]);
}
}
}
private static void print(int[][] arr) {
System.out.println(&quot;***********PRINT***********&quot;);
for(int i=0; i&lt;arr.length; i++) {
for(int j=0; j&lt;arr[i].length; j++) {
System.out.print(arr[i][j]+&quot; &quot;);
}
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 

huangapple
  • 本文由 发表于 2020年8月31日 13:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63665224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定