【问题标题】:Constructor of a 2d array only contains the last value二维数组的构造函数只包含最后一个值
【发布时间】:2013-09-09 05:16:09
【问题描述】:

我正在使用扫描仪读取文件。该文件被格式化,第一行是数组的维度。下一行包含第一行,下一行是第二行,依此类推。示例文件:

3 3 1 2 3 4 5 6 7 8 9

我一直遇到的问题是我的数组值似乎都是 9。文件是整数,但我需要双精度数。当我试图调试正在发生的事情时,我也有很多打印语句。我的异常处理还没有完成。在我可以正确地实例化数组之后,我会回去加强它。任何指针将不胜感激。例如:只需打开文件一次即可获取维度和实例化数组的更好方法。

已更新但收到 nullPointerException

public class Help implements TopoMapInterface {

private String filename;
private File mapfile;
public double[][] baseMap;
public double[][] enMap;
public int enhancementLevel;

public Help(String filename) throws FileNotFoundException,
        InvalidFileFormatException {
    this.filename = filename;

    System.out.println("Reading in file: " + filename);

    String number = "";
    int row = 0;
    int col = 0;
    int count = 0;

    try {
        Scanner inputFile = new Scanner(new File(filename));

        while (inputFile.hasNextInt()) {
            row = Integer.parseInt(inputFile.next());
            col = Integer.parseInt(inputFile.next());
            System.out.println("Row : " + row);
            System.out.println("Col : " + col);
            baseMap = new double[row][col];
            System.out.println(baseMap[2][4]);
            for (int i = 0; i < baseMap.length; i++){
                for (int j = 0; j < baseMap[i].length; j++){
                    baseMap[i][j] = Double.parseDouble(inputFile.next());
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }

【问题讨论】:

    标签: java arrays file-upload multidimensional-array


    【解决方案1】:

    假设你的第一行的值是先行后列

    我会这样做

    int row = Double.parseDouble(inputFile.next());
    int col = Double.parseDouble(inputFile.next());
    
    
    for (int i = 0;i<row;i++){
    for(j=0;j<col;j++)
    {
    baseMap[i][j]=Double.parseDouble(inputFile.next());
    }}
    

    这应该根据需要将所有值存储为双倍,我认为这是从文件读取后存储的更简单方法。

    我认为你的问题是正确的!

    【讨论】:

    • 我要这样做。我在这里发布了一个新的直接问题link。如果你想看看。谢谢
    【解决方案2】:

    每次读取count1 &gt; 2 时的数字时,您都会遍历整个矩阵并将doubleVal 插入每个单元格;你看到的最后一个值是 9,所以这就是你到处都有的值。

    更一般地说,如果您保证输入正确(即,您有这样的学校练习),那么您不应该在循环中阅读您的尺寸规格;您应该阅读前两个ints,根据该大小创建数组,然后使用嵌套的for 循环将值插入到数组中。

    【讨论】:

    • 如果它对你写作有帮助,[1,1,...,1] 然后写 [2,2,...,2] 然后最终写 [9.9,... ,9]。
    【解决方案3】:
    for (int r = 0; r < baseMap.length; r++){
        for (int c = 0; c < baseMap[r].length; c++){
            baseMap[r][c] = doubleVal;
        }
    }
    

    是问题所在。 这遍历数组的每一行和每一列,将每个元素设置为当前数字 因为你看到的最后一个数字是 9,所以这就是数组中剩下的数字。

    您真正需要的是跟踪行数和列数的其他方法,而不是遍历数组。也许是一对计数器?

    int intVal = Integer.parseInt(number);
    double doubleVal = (double) intVal;
    

    应该替换为 double doubleVal = Double.parseDouble(number);

    No Idea For Name 给出的文件读取改进 将显着改进此代码。虽然 using 是 java 7 和更高版本的唯一构造。 对于早期版本。可以升级,否则记得关闭资源。

    【讨论】:

    • 无意在这里发表评论。对不起
    【解决方案4】:

    首先,当使用scanner 或任何其他流时,您应该在最后使用finally 语句关闭它们

    Scanner inputFile = null;
    
    try{
      inputFile = new Scanner(new File(filename));
    }
    catch{}
    finally{
       if(inputFile != null)
          inputFile.close();
    }
    

    这将确保您在完成读取后释放扫描仪,而不是将其拖到下一个循环。

    另外,在您的代码中,在第二个 while 循环中,您似乎没有正确关闭它,因此在每个循环中都会调用此代码:

        if (count1 > 2){
            for (int r = 0; r < baseMap.length; r++){
                for (int c = 0; c < baseMap[r].length; c++){
    
                baseMap[r][c] = doubleVal;
            }
        }  
    

    最后一件事,您打开文件两次!没有必要这样做。您至少可以将代码更改为:

    public class Help implements TopoMapInterface {
    
    private String filename;
    private File mapfile;
    public double[][] baseMap;
    public double[][] enMap;
    public int enhancementLevel;
    
    public Help(String filename) throws FileNotFoundException,
            InvalidFileFormatException {
        this.filename = filename;
    
        System.out.println("Reading in file: " + filename);
    
        String number = "";
        int row = 0;
        int col = 0;
        int count = 0;
        Scanner inputFile = null;
    
        try {
            inputFile = new Scanner(new File(filename));
                number = inputFile.next();
                System.out.println(number);
                row = Integer.parseInt(number);
                number = inputFile.next();
                col = Integer.parseInt(number);
                int count1 = 0;
                baseMap = new double[row][col];
                while (inputFile.hasNextInt()) {
                    count1++;
                    number = inputFile.next();
                    int intVal = Integer.parseInt(number);
                    double doubleVal = (double) intVal;
                }// Closed your while loop here
    
                if (count1 > 2){
                    for (int r = 0; r < baseMap.length; r++){
                        for (int c = 0; c < baseMap[r].length; c++){
    
                        baseMap[r][c] = doubleVal;
                    }
                }   
                System.out.println(doubleVal+"*");
                }   
    
            }
            System.out.println("end of this while loop");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        finally
        {
            if(inputFile != null)
               inputFile.close();
        }
    
        try {
            System.out.println("Row = " + row + "  Col = " + col);
            for (int r = 0; r < baseMap.length; r++) {
                for (int c = 0; c < baseMap[r].length; c++) {
                    System.out.print(baseMap[r][c] + " ");
                }
            }
    
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
    

    【讨论】:

    • 您是否将 Java 与 C# 混淆了?
    【解决方案5】:

    我不确定您为什么要扫描文件两次,但如果您这样做了,那么这就是您的代码中的问题

    for (int r = 0; r < baseMap.length; r++){
        for (int c = 0; c < baseMap[r].length; c++){
            baseMap[r][c] = doubleVal;
        }
    }
    

    以下代码将解决您的问题:

    try {
            Scanner inputFile = new Scanner(new File(filename));
            int count1 = 0;
            int r = -1, c = -1;
            baseMap = new double[row][col];
            while (inputFile.hasNextInt()) {
                count1++;
                number = inputFile.next();
                int intVal = Integer.parseInt(number);
                double doubleVal = (double) intVal;
    
    
                if (count1 > 2){
                    if (count1 % row == 0)
                        r++;
                    c = count1 % col;
                    baseMap[r][c] = doubleVal;
    
    
                System.out.println(doubleVal+"*");
                }   
            }
    
            inputFile.close();
    
            System.out.println("Row = " + row + "  Col = " + col);
            for (r = 0; r < baseMap.length; r++) {
                for (c = 0; c < baseMap[r].length; c++) {
                    System.out.print(baseMap[r][c] + " ");
                }
            }
    
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    

    别忘了在你的第一次扫描中休息一下

    if (count == 1) {
         row = Integer.parseInt(number);
    } else if (count == 2) {
         col = Integer.parseInt(number);
         break;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 2018-04-20
      • 2019-01-09
      • 2023-04-08
      • 1970-01-01
      • 2019-11-29
      相关资源
      最近更新 更多