ArrayList returning null values

-2

I have an array, I add values in this array, I send them to a method in another class that puts the values of this array in a string only and I add this single string in an ArrayList, but when I go to show it, it shows only several nulls ".

Main class:

class Principal {
    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        Logica chamar = new Logica(valores);
        String[][] valores = new String[3][2];
        ArrayList colecao = new ArrayList();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                if (j == 0) {
                    System.out.println("Insira um nome.");
                } else {
                    System.out.println("Insira um número.");
                } valores[i][j] = entrada.next();
            }
        } colecao.add(chamar);
        for (Object resolucao:colecao) {
            System.out.println(resolucao.toString());
        }
    }
}

Logical class:

class Logica {
    String valores;
    public Logica(String[][] valores) {
        valores = new String[3][2];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                this.valores = valores[i][j] + "\n";
            }
        }
    }
    public String toString() {
        return valores;
    }
}

What's wrong?

    
asked by anonymous 20.04.2014 / 18:32

1 answer

4

Your code has some errors, I'll explain the modifications that would need to be made:

Main class:

Your code demonstrates a custom, possibly inherited from C programming, which is to declare all variables at the beginning of the method before using them. In Java you are not obliged to do this, it can even cause problems in your logic, for example in this section:

Logica chamar = new Logica(valores);
String[][] valores = new String[3][2];

This does not even compile. You are passing the variable valores to the constructor of class Logica before even valores is declared. I think what you want is to call this line:

Logica chamar = new Logica(valores);

just below the for threaded, that is, just above this other line:

colecao.add(chamar);

With this, your chamar object will get the valores already populated by the user, which does not happen when new Logica(valores) is executed before for .

Logical class:

This class has a line that should not exist ...

valores = new String[3][2];

... because in this line you lose the values passed to class Logica through its constructor. There is already a line initializing the valores variable with the [3][2] sizes in the main class, you do not need to do this here again. Rebooting the variable this way causes valores to refer to a new array object that will not contain the values populated by the user; therefore, this line should be removed.

Finally, if you want to concatenate each value and form a single large string with the concatenated values, you should change this line:

this.valores = valores[i][j] + "\n";

by this:

this.valores = this.valores + valores[i][j] + "\n";

or by a version that uses the += operator:

this.valores += valores[i][j] + "\n";

One last recommendation: you are using the same name ( valores ) for a variable that is the attribute of your Logica class and another that is the constructor parameter of that class. Avoid using the same name in both, as this makes the code less readable and can even cause confusion and logical errors when the code needs to be modified. Using different names for each of the variables you are even free to use this to specify that you are referring to one and not the other in your code.

    
20.04.2014 / 19:06