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

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

    Since you only need to store booleans in that 2D array, I would say the most suitable data structure (as both memory consumption and useful interface) would be a java.util.BitSet, which is basically a class that models a bit array:

    As it's a 2D array, I think the way to go would be a:

    List bitArrays = new ArrayList();
    

    In the List, you cannot just say: "here's the 5th element" without inserting the first 4 elements. But in the BitSet, you can simply set() whatever bit you need and it will automatically expand to the required size.

提交回复
热议问题