How to get a two-dimensional array from an arrayList public ArrayListString [] []

0

I have the structure of the following function:

public String [][] obtieneMinimoEstadoHijos(ArrayList<String[][]>listaEstadosHijos){


}

I've searched for information on how to print the matrices contained in an arrayList. But I can not find it.

    
asked by Eduardo Tolentino 16.09.2018 в 13:24
source

1 answer

1
  

I've searched for information on how to print the matrices contained in a ArrayList . But I can not find it.

You will have to obtain each of the matrices of your ArrayList<String[][]> in each route of your loop , and go through another loop of each of the arrays contained in the matrix, and the same for each String of each array .

for (String[][] matriz : listaEstadosHijos) {
        for (String[] array : matriz) {
             for (String str : array) {
                 System.out.print(str + " ");
             }
             System.out.println("");
        }
        System.out.println("");
}

I'll give you an example:

public class RecorrerBiDimensional {
    public static void main(String[] args) {
        String[][] matriz1 = {
            {"Diego", "Pedro", "Fernando"},
            {"Ana", "Luis", "Antonio"},
            {"Sonia", "Federico", "Bartolo", "Africa"}
        };

        String[][] matriz2 = {
            {"Eva", "Lucia", "Cristiano"},
            {"Messi", "Isco", "Tico", "Lopetegui"},
            {"Luna", "Amelia", "Bale", "Sol"}
        };

        List<String[][]> listaEstadosHijos = new ArrayList<>();
        //añadimos cada una de las matrices a nuestra lista.
        listaEstadosHijos.add(matriz1);
        listaEstadosHijos.add(matriz2);

        //Recorremos cada una de las matrices e imprimimos resultado            
        for (String[][] matriz : listaEstadosHijos) {
            for (String[] array : matriz) {
                for (String str : array) {
                    System.out.print(str + " ");
                }
                System.out.println("");
            }
            System.out.println("");
        }

    }
}

Result:

Diego Pedro Fernando 
Ana Luis Antonio 
Sonia Federico Bartolo Africa 

Eva Lucia Cristiano 
Messi Isco Tico Lopetegui 
Luna Amelia Bale Sol 
    
answered by 16.09.2018 в 16:13