How to fill a two Dimensional ArrayList in java with Integers?

前端 未结 8 2456
迷失自我
迷失自我 2020-12-15 01:57

I have to create a 2d array with unknown size. So I have decided to go with a 2d ArrayList the problem is I\'m not sure how to initialize such an array or store information.

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 02:04

    The short answer to the followup question is

    array.get(i1).put(i2, value);
    

    but both the get and put could fail if the size of the ArrayList is <= the index. So if you want to be able to fill in arbitrary values, you need to write methods to expand as necessary. The call would then be something like

    putCell(getRow(array, i1), i2, value)
    

    where getRow() knows how to grow the ArrayList of ArrayList, and putCell() knows how to grow the ArrayList.

提交回复
热议问题