Two-dimensional array size not being defined

0

I created an array and put it to initialize with the values passed in the constructor, but it is not initializing. I created the object, I threw the values, but when I call the method to show the matrix, it does not appear, as if it had not been created. Can someone help me?

import java.util.Scanner;

public class ExerMatriz {
    Scanner s = new Scanner(System.in);

    private int linha;
    private int coluna;

    ExerMatriz(){
        System.out.println("Digite a quantidade de linhas");
        this.setLinha(s.nextInt()); 
        System.out.println("Digite a quantidade de colunas");
        this.setColuna(s.nextInt()); 

    }


    private int m[][] = new int[this.getLinha()][this.getColuna()];

    public void mostrarMatriz(){
        for(int i=0; i< m.length;i++){
            for(int j=0; j<m[0].length;j++){
                System.out.print(this.m[i][j]);
            }
            System.out.println();
        }
    }


    public int[][] getM() {
        return m;
    }

    public void setM(int[][] m) {
        this.m = m;
    }
    public int getLinha() {
        return linha;
    }

    public void setLinha(int linha) {
        this.linha = linha;
    }

    public int getColuna() {
        return coluna;
    }

    public void setColuna(int coluna) {
        this.coluna = coluna;
    }
}
    
asked by anonymous 16.03.2017 / 21:12

1 answer

0

The problem is that you are starting and setting the size of the vector dimensions when instantiating your class. When this.getLinha() and this.getColuna() methods are invoked on the line:

private int m[][] = new int[this.getLinha()][this.getColuna()];

They return 0, because linha and coluna are variables of type int , and numeric primitives like int are started by java with value 0. So its array has dimensions 0x0.

This line is executed when the ExerMatriz class is instantiated, that is, when you do ExerMatriz e = new ExerMatriz() , and it is no good defining the size afterwards, the attribute is already created and started.

I suggest you create a constructor where you start the vector already with the sizes collected from Scanner . Another point within the class itself is that there is little need for it to use its own get and set , since it can access its own attributes directly:

import java.util.Scanner;

public class ExerMatriz {
    Scanner s = new Scanner(System.in);

    private int linha;
    private int coluna;

    public ExerMatriz(){
        System.out.println("Digite a quantidade de linhas");
        this.linha(s.nextInt()); 
        System.out.println("Digite a quantidade de colunas");
        this.coluna(s.nextInt()); 
        this.m = new int[linha][coluna];
    }



    private int m[][];

    public void mostrarMatriz(){
        for(int i=0; i< m.length;i++){
            for(int j=0; j<m[0].length;j++){
                System.out.print(this.m[i][j]);
            }
            System.out.println();
        }
    }


    public int[][] getM() {
        return m;
    }

    public void setM(int[][] m) {
        this.m = m;
    }
    public int getLinha() {
        return linha;
    }

    public void setLinha(int linha) {
        this.linha = linha;
    }

    public int getColuna() {
        return coluna;
    }

    public void setColuna(int coluna) {
        this.coluna = coluna;
    }
}

It is also worth mentioning that this array is being created empty, because at any moment of the code it is filled with values.

    
16.03.2017 / 21:51