简体   繁体   English

使用Java文件中的值制作2d int数组

[英]make 2d int array with values from file in java

I'm having problems storing a file that contains integers into a 2d int array with those values. 我在将包含整数的文件存储到具有这些值的2d int数组时遇到问题。 The file has this format: 该文件具有以下格式:

 1  19      36 
 1   4     212
 1   2     732
 2   9     111
 2   1      66
 2  12      29 
 2  19      14
 2  17      65
 3   2      17
 3  11      38
 3  14     122
 3  17     211
 3   1     390
 3  18      78
 3   9      11
 4   3     273  
 4   5      29   
 4  12      42 
 5   4     122
 5  16      12

Note there are some trailing spaces on some lines. 请注意,某些行上有一些尾随空格。 You can check out the full file here . 您可以在此处签出完整文件。

This is what I have so far. 到目前为止,这就是我所拥有的。 I read the file and put each line in an array list. 我读取了文件并将每一行放入数组列表中。 I thought this would be easier because then I would just be able to parse the integers out and put them into the 2d array but I've been having problems. 我以为这会更容易,因为那样我便可以解析出整数并将其放入2d数组中,但是我一直遇到问题。 Any suggestions? 有什么建议么?

private void readRoadFile()
{
    String line = null;
    try
    {
        FileReader fr = new FileReader("road.dat");
        BufferedReader br = new BufferedReader(fr);
        while((line = br.readLine()) != null)
        {
            roadInfo.add(line);
        }
        br.close();
    } catch(FileNotFoundException e)
    {
        System.out.println("File does not exist");
    } catch(IOException e)
    {
        System.out.println("Error reading file");
    }
}

You should parse strings and transform your arraylist into an array: 您应该解析字符串并将arraylist转换为数组:

    List<int[]> roadInfo = new ArrayList<>();

    try (BufferedReader reader = new BufferedReader(new FileReader("road.dat"))) {

        for (String line = reader.readLine(); line != null; line = reader.readLine()) {

            String[] split = line.split("\t"); // or \\s+
            int[] numbers = new int[split.length];

            for (int i = 0; i < split.length; i++) {
                numbers[i] = Integer.parseInt(split[i].trim());
            }

            roadInfo.add(numbers);
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    int[][] twoDArray = roadInfo.toArray(new int[roadInfo.size()][]);

You can use the following solution - it uses the modern Java 8 Streams to simplify the flow of data. 您可以使用以下解决方案-它使用现代的Java 8 Streams来简化数据流。

@Override
public void run()
{
    List<List<Integer>> result = new ArrayList<>();
    try (FileInputStream fileInputStream = new FileInputStream("stackoverflow-q-34096490.txt");
         InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
         BufferedReader bufferedReader = new BufferedReader(inputStreamReader))
    {
        for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine())
            result.add(Arrays.stream(line.split("\\s+")).filter(s -> !s.isEmpty()).map(Integer::parseInt).collect(Collectors.toList()));
    } catch (IOException ioe)
    {
        System.err.println("Read failure!");
        ioe.printStackTrace();
    }

    // use result
    System.out.println(result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM