Content of one matrix receive the other

1

Parent Matrix:

    public static String [][] matrizPrincipal(){
        String [] [] matriz = new String [2] [6];
        matriz[0][0] = "Robin Arryn";
        matriz[0][1] = "Lino Facioli";
        matriz[0][2] = "145";
        matriz[0][3] = "Vivo";
        matriz[0][4] = "Arryn";
        matriz[0][5] = "Masculino";

        matriz[1][0] = "Yohn Royce";
        matriz[1][1] = "Rupert Vansittart";
        matriz[1][2] = "45";
        matriz[1][3] = "Vivo";
        matriz[1][4] = "Arryn";
        matriz[1][5] = "Masculino";
        return matriz;
    }

I'm trying to create a new array that stores only content that is in column 0, that is, in the matriz[i][0]

But I'm not getting success in the new method. I'm trying to create it like this:

public static String [] [] novaMatriz(String [] [] mat){
        String [] [] matriz2 = new String [mat.length] [1];
        for(int i=0; i<mat.length; i++){
                matriz2[i][0] = mat[i][0];
        }
        return matriz2;
}

What would be the problem? how to proceed When I try to call this return results in this:

[[Ljava.lang.String;@dc0772
    
asked by anonymous 17.05.2015 / 00:18

1 answer

1

You have two problems here.

First, if your array at the end will have only one column, then it does not need to have two dimensions, it's easier to work with just one:

public static String[] pegaPrimeiraLinha(String[][] mat) {
    String[] matriz2 = new String[mat.length];
    for (int i = 0; i < mat.length; i++) {
        matriz2[i] = mat[i][0];
    }
    return matriz2;
}

The second problem is that doing this:

String[] array = ...; // Ou pode ser com duas ou mais dimensões também que vai dar na mesma.
System.out.println(matriz);

In order for the array (or any other object passed to System.out.println ) to be converted to a String , the toString() method will be called. And that's where the problem lies. Arrays do not implement method toString() ! As a result, the implementation called will be that of class Object . The implementation of toString() of class Object only returns the class name followed by an at and hashCode in hexadecimal, which is not a very useful representation.

What you have to do then is to use a method that converts to String in a way that makes sense. In the java.util.Arrays class there is a method toString(Object[]) that will serve your purpose. This way your program will look like this:

import java.util.Arrays;

public class Matrizes {
    public static String[][] matrizPrincipal() {
        String[][] matriz = new String[2][6];
        matriz[0][0] = "Robin Arryn";
        matriz[0][1] = "Lino Facioli";
        matriz[0][2] = "145";
        matriz[0][3] = "Vivo";
        matriz[0][4] = "Arryn";
        matriz[0][5] = "Masculino";

        matriz[1][0] = "Yohn Royce";
        matriz[1][1] = "Rupert Vansittart";
        matriz[1][2] = "45";
        matriz[1][3] = "Vivo";
        matriz[1][4] = "Arryn";
        matriz[1][5] = "Masculino";
        return matriz;
    }

    public static String[] pegaPrimeiraLinha(String[][] mat) {
        String[] matriz2 = new String[mat.length];
        for (int i = 0; i < mat.length; i++) {
            matriz2[i] = mat[i][0];
        }
        return matriz2;
    }

    public static void main(String[] args) {
        String[][] matriz = matrizPrincipal();
        String[] reduzida = pegaPrimeiraLinha(matriz);
        System.out.println(Arrays.toString(reduzida));
    }
}

See here working on IdeOne.

    
17.05.2015 / 05:22