Content of one matrix receiving from another

Asked

Viewed 270 times

1

Main 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 am trying to create a new matrix that stores only the content that is in column 0, that is, in position matriz[i][0]

But I’m not succeeding 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 that:

[[Ljava.lang.String;@dc0772
  • String [] [] matriz2 = new String [2] [2]; should not be String [] [] matriz2 = new String [mat.length] [2]; ?

  • I think the right thing would be [mat.length][1], because I only need one column to store the names. But the problem persists..

1 answer

1


You got two problems here.

First, that if your matrix at the end will have only one column, then it does not need to have two dimensions, it becomes easier to work using only 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 in doing so:

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

So that the matrix (or any other object passed to the System.out.println) be converted to a String, the method toString() this will be called. And here is the problem. Arrays do not implement the method toString()! As a result the called implementation will be that of the class Object. To implementation of toString() class Object only returns the class name followed by an arroba and the hashCode hexadecimal, which is not a very useful representation.

What you have to do then is use a method that does the conversion to String in a way that makes sense. In class java.util.Arrays 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.

  • The program returned the following error: http://puu.sh/hQpgX/1dbb696b40.png

  • 1

    Make sure you have the import line at the beginning of the code: import java.util.Arrays;

  • 1

    @Guilherme As Otávio said, make sure you haven’t forgotten the import java.util.Arrays;

  • That’s right, it worked! Thank you very much

Browser other questions tagged

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