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

前端 未结 8 2417
迷失自我
迷失自我 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:07

    If you don't have all of the data in advance to be able to leverage aioobe's solution, you could use a Table from Google's Guava library.

    Table matrix = new HashBasedTable();
    matrix.put(rowIndex,columnIndex,value);
    

    The primary drawback of this is that it's not incredibly fast or memory efficient if you are working with a large quantity of data, as everything is a hash lookup and primitives are wrapped by Integer.

提交回复
热议问题