Two-dimensional array size is not being defined

Asked

Viewed 266 times

0

I created a matrix and put it to initialize with the values passed in the constructor, but it is not initializing. I created the object, played the values, but when I call the method to show the matrix, it does not appear, as if it had not been created. Someone can 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;
    }
}
  • Diego, thank you very much. I thought earlier about what you said, that she was a servant, but I didn’t know how to do it. If you can only ask one more question... The second is, (int j=0; j<m[0].length;j++), picks up the column. However, [0]. length only picks up if it is of the same row dimension, correct? How do I if it’s different sizes?

  • Rereading your doubt, I realize I didn’t understand correctly, you can ask a new question to clarify this :)

1 answer

0


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

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

they return 0, because linha and coluna are variables of the type int, and primitive numerical types such as int are started by java with value 0. Then its matrix has dimensions 0x0.

This line is executed when the class ExerMatriz is instantiated, ie when you do ExerMatriz e = new ExerMatriz(), and it is no use to set the size later, the attribute is already created and started.

I suggest you create a constructor by which you start the vector already with the collected sizes of the Scanner. Another point, within the class itself, there is not much need for it to use its own get and set, once she can access her 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 matrix is being created empty, because at some point of the code it is filled with values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.