How do I loop thorough a 2D ArrayList in Java and fill it?

前端 未结 3 1096
悲哀的现实
悲哀的现实 2021-01-16 16:14

I am trying to use 2D arrayLists in Java. I have the definition:

ArrayList> myList = new ArrayList

        
3条回答
  •  粉色の甜心
    2021-01-16 16:27

    Assuming the matrix is not initialized,

    int m = 10, n = 10;
    ArrayList> matrix = new ArrayList>();
    
    for (int i = 0; i < m; i++) {
        List row = new ArrayList();
        for (int j = 0; j < n; j++) {
            row.add(j);
        }
        matrix.add(row);
    }
    

提交回复
热议问题