Two dimensional array list

前端 未结 9 1273
梦毁少年i
梦毁少年i 2020-11-28 05:09

I\'ve heard of using a two dimensional array like this :

String[][] strArr;

But is there any way of doing this with a list?

Maybe s

9条回答
  •  春和景丽
    2020-11-28 05:26

    Declaring a two dimensional ArrayList:

    ArrayList> rows = new ArrayList();
    

    Or

    ArrayList> rows = new ArrayList<>();
    

    Or

    ArrayList> rows = new ArrayList>(); 
    

    All the above are valid declarations for a two dimensional ArrayList!

    Now, Declaring a one dimensional ArrayList:

    ArrayList row = new ArrayList<>();
    

    Inserting values in the two dimensional ArrayList:

    for(int i=0; i<5; i++){
        ArrayList row = new ArrayList<>();
        for(int j=0; j<5; j++){
            row.add("Add values here"); 
        }
        rows.add(row); 
    }
    

    fetching the values from the two dimensional ArrayList:

    for(int i=0; i<5; i++){
        for(int j=0; j<5; j++){
            System.out.print(rows.get(i).get(j)+" ");
         }
         System.out.println("");
    }
    

提交回复
热议问题