使用Java中的构造函数初始化2-D数组

时间:2019-02-05 16:46:04

标签: java arrays constructor

Matrix.java

    import java.io.*;
    class Matrix {
        private int q[][];

        public Matrix() {
            for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
                q[i][j] = Integer.parseInt(System.console().readLine());    
        }


        public Matrix( int a , int b ) {    
            int mat[][] = new int [a][b];
            for(int i=0; i<mat.length; i++) {
                for(int j=0;j<mat[i].length;j++)
                    q[i][j] = Integer.parseInt(System.console().readLine());
            }
        }



         public void show() {
            for(int i=0; i<q.length; i++) {
                for(int j=0;j<q[i].length;j++)
                    System.out.println(q[i][j]+" ");
            }   
        }
    }

UseMatrix.java

class UseMatrix {
    public static void main(String args[]) {

        Matrix m1 = new Matrix();  
        System.out.println("First Matrtix ");
        m1.show();

        Matrix m2 = new Matrix(5,4);
        System.out.println("Second Matrtix "); 
        m2.show();


    }
}
  

该程序在运行时显示NullPointerException错误

     

困惑为什么不起作用可能需要一点帮助,我想通过默认构造函数创建大小为3 * 3的2D数组。

     

然后我要使用参数化构造函数创建大小为5 * 4的数组。

1 个答案:

答案 0 :(得分:0)

存在许多问题:

  • 首先:private int q[][]; // this holds a null value, never assigned on the parameterless constructor。一种可能的解决方案是在构造函数中添加q[][] = new int[a][b];q[i] = new int[b];// each time you enter the loop。为清楚起见,请参见下面的代码。

  • 第二步,您在public Matrix() {中尝试访问数组内容而不检查其长度(for(int i=0;i<3;i++)从0变为3,与实际数组大小有关)。数组在开始时为空,因此会在此处q[i][j] = Integer.parseInt...导致另一个NullPointerException(我将通过重用其他构造函数来解决此问题,因为他们想获得相同的结果)

  • 第三there is problems reading from console(),所以我也做了更改。 (并在您要求用户输入数字的位置添加了一行)

  • 我所做的最后更改是对show方法的一小部分调整。

结果:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class App {
    public static void main(String[] args) {

        Matrix m1 = new Matrix();
        System.out.println("First Matrtix ");
        m1.show();

        Matrix m2 = new Matrix(5,4);
        System.out.println("Second Matrtix ");
        m2.show();
    }


}

class Matrix {
    private int q[][];
    private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public Matrix() {
        this(3, 3);
    }


    public Matrix(int a, int b ) {
        int value;
        System.out.println("Input a number to fill the new 3x3 matrix");
        try {
            value = Integer.parseInt(br.readLine());
        } catch (IOException e) {
            throw new RuntimeException("There was a problem reading the number from console", e);
        }
        q = new int[a][b];
        for(int i=0;i<a;i++) {
            q[i] = new int[b];
            for(int j=0;j<b;j++) {
                q[i][j] = value;
            }
        }
    }



    public void show() {
        for(int i=0; i<q.length; i++) {
            for(int j=0;j<q[i].length;j++)
                System.out.print(q[i][j]+" ");
            System.out.print("\n");
        }
    }
}

Program's output