【问题标题】:Stuck with 2D Array卡在二维数组中
【发布时间】:2016-01-28 20:40:08
【问题描述】:

我正在尝试编写一个程序来读取包含这样的双精度列表的文本文件(很好):

1.0, 2.0, 3.0
4.0, 5.0, 6.0
7.0, 8.0, 9.0

_

public static void main(String[] args) {

    Scanner inputStream = null;

    try {
        inputStream = new Scanner(new FileInputStream("tstc.txt"));
    }
    catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.out.println("Program aborted.");
        System.exit(0);
    }

    double[][] arrayNums = new double[3][3];

    for(int sec = 0; sec < 3; sec++) {
        while(inputStream.hasNextLine()) {
            String line = inputStream.nextLine();
            String[] lineList = line.split(", ");
            int pos = 0;

            for(int motor = 0; motor < 3; motor++) {
                double lineDouble = Double.parseDouble(lineList[pos]);
                arrayNums[motor][sec] = lineDouble;
                pos+=1;
            }
        }
    }
    System.out.println(Arrays.deepToString(arrayNums));

这就是我想要的:

    [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]

但这就是我得到的:

[[7.0, 0.0, 0.0], [8.0, 0.0, 0.0], [9.0, 0.0, 0.0]]

我已经看到我的问题在于 sec 变量保持不变,但我不确定如何在不必更改整个代码结构的情况下重新排列代码。

任何提示将不胜感激。

【问题讨论】:

  • 你不需要 sec 循环,因为你已经有了 InputStream 一个

标签: java for-loop multidimensional-array


【解决方案1】:

您需要删除 while 循环。

这会给你预期的结果:[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class ReadMatrix {

    public static void main(String[] args) {

        String fileName = "tstc.txt";
        int nRow = 3;
        int nCol = 3;
        double[][] arrayNums = new double[nRow][nCol];

        try (Scanner inputStream = new Scanner(new FileInputStream(fileName))) {

            for (int i = 0; i < nRow; i++) {
                String line = inputStream.nextLine();
                String[] lineList = line.split(", ");

                for (int j = 0; j < nCol; j++) {
                    double lineDouble = Double.parseDouble(lineList[j]);
                    arrayNums[i][j] = lineDouble;
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            System.out.println("Program aborted.");
            return;
        }
        System.out.println(Arrays.deepToString(arrayNums));
    }
}

【讨论】:

    【解决方案2】:
    int sec = 0;
    while (inputStream.hasNextLine()) {
        String line = inputStream.nextLine();
        String[] lineList = line.split(", ");
    
        for (int i = 0; i < lineList.length; i++) {
            arrayNums[sec][i] = Double.parseDouble(lineList[i]);
        }
    
        sec++;
    }
    

    也就是说,如果您没有预料到文件中的行数(即文件中的行数多于数组中的行数),则会在内部 for 循环中引发异常

    【讨论】:

    • 首先,非常感谢您的帮助!问题是,事实上,我将要从中读取的文本文件将有数百列(要读取的行)。这就是为什么我真的想像这样构造arrayNums:arrayNums[i][sec]。为了节省内存以及更实用和易于导航。 [i] 实际上应该代表汽车,而 [sec] 代表速度。文件中的每一行都是随后的一秒,说明汽车的速度。有没有办法适应这个?抱歉之前没说清楚。
    【解决方案3】:

    这应该像预期的那样工作。

        int pos = 0;
        while(inputStream.hasNextLine()) {
            String line = inputStream.nextLine();
            String[] lineList = line.split(", ");
    
            for(int motor = 0; motor < 3; motor++) {
                double lineDouble = Double.parseDouble(lineList[motor]);
                arrayNums[pos][motor] = lineDouble;
    
            }
            pos++;
        }
    System.out.println(Arrays.deepToString(arrayNums));
    

    不要忘记删除第一个循环。

    【讨论】:

    • 首先,非常感谢您的帮助!问题是,事实上,我将要从中读取的文本文件将有数百列(要读取的行)。这就是为什么我真的想像这样构造arrayNums:arrayNums[motor][pos]。为了节省内存以及更实用和易于导航。 [motor] 实际上应该代表汽车,而 [pos] 代表速度。文件中的每一行都是随后的一秒,说明汽车的速度。有没有办法适应这个?抱歉之前没说清楚。
    • @AndréFoote 如果我理解得足够好,您应该更改变量名称。但是,如果您还有其他问题,请不要犹豫,创建一个新问题,因为这个问题已经得到解答,它也会得到其他用户的更多关注。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2020-07-30
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 2016-02-24
    • 2020-12-04
    相关资源
    最近更新 更多