call a constructor from another builder in java

0

for a practice exercise I have two constructors to create a table, in one of them I receive as arguments a matrix with the data and a vector with the headers, I want to insert the headers in the matrix and then send the edited matrix to the final constructor, but I was informed that I can not put logic in the constructor, so I took the process apart in a method which edits the matrix and returns it to send it to the final constructor, but I still get an error, I do not know what is wrong and What can you recommend me to do?

public Tabla(String [][]matriz){
    filas=matriz.length; //devuelve cantidad de filas en la matriz
    columnas=matriz[0].length; //devuelve cantidad de datos enesa fila
    m=new String[filas][columnas];
    m=matriz;
    m2=new String[filas][columnas];
    m2=matriz;  //uso m2 para cuando se necesite girar la tabla
    bordeH="";
    bordeV="|";
    tabla="";
    anchoColumna=new int[columnas]; //dimensiona segun cantidad decolumnas
    anchoFila=new int[filas];
    anchoTabla=0;
}
public Tabla(String [][] matriz, String[]encabezado){
    Tabla(unirMatriz(matriz,encabezado));
}
    
asked by alf VI 11.09.2018 в 15:39
source

1 answer

0

to call the first constructor you must use the reserved word this. In such a way that if we call the second constructor as I put you in the example. Create an instance of the first constructor. To check it I put these lines in the first constructor:

System.out.println(">>>> estamos en constructor 1");
System.out.println(">>>> salimos del constructor 1");

This would be the console output:

run:
>>>> estamos en constructor 1
>>>> salimos del constructor 1
Filas >>>> 9
Columnas >>>> 9
Tablas unidas
BUILD SUCCESSFUL (total time: 0 seconds)

This would be the main class:

public class StackOverFlow {

    public static void main(String[] args) {

        String[][] matriz = new String[9][9];
        String[] encabezado = new String[9];

        Tabla tabla = new Tabla(matriz,encabezado);
    }

}

To verify that the method call works, we verify that the variables have been instantiated well, taking their length by console:

public class Tabla {
    private int filas;
    private int columnas;
    private String[][] m;
    private String[][] m2;
    private String bordeH;
    private String bordeV;
    private String tabla;
    private int[] anchoColumna; 
    private int[] anchoFila;
    private int anchoTabla;
    private String[] encabezado;

    public Tabla(String [][]matriz){
        System.out.println(">>>> estamos en constructor 1");
        filas=matriz.length; //devuelve cantidad de filas en la matriz
        columnas=matriz[0].length; //devuelve cantidad de datos enesa fila
        m = new String[filas][columnas];
        m = matriz;
        m2 = new String[filas][columnas];
        m2=matriz;  //uso m2 para cuando se necesite girar la tabla
        bordeH="";
        bordeV="|";
        tabla="";
        anchoColumna=new int[columnas]; //dimensiona segun cantidad decolumnas
        anchoFila=new int[filas];
        anchoTabla=0;
        System.out.println(">>>> salimos del constructor 1");
    }

    public Tabla(String [][] matriz, String[]encabezado){
        this(matriz);
        unirMatriz(matriz, encabezado);

    }

    public void unirMatriz(String [][] matriz,String[]encabezado){
        System.out.println("Filas >>>> "+filas);
        System.out.println("Columnas >>>> "+columnas);
        System.out.println("Tablas unidas");
    } 
}
    
answered by 11.09.2018 в 19:03