How to make ArrayList that work as two dimentional array in java?

后端 未结 7 1732
时光说笑
时光说笑 2021-01-23 20:16

I want to make arrayList object in java that work as two dimentional array. My question is how can we access value from specific dimention from arrayList.

in two diment

7条回答
  •  萌比男神i
    2021-01-23 20:58

    Memory is an important consideration here.

    It can be acceptable to model a 2D (or higher dimension) array using a 1D container. (This is how the VARIANT SAFEARRAY of Microsoft's COM works.) But, consider this carefully if the number of elements is large; especially if the container allocates a contiguous memory block. Using something like List will model a jagged-edged matrix and can fragment your memory.

    With the 1D approach, you can use the get(index) method on the ArrayList appropriately transformed:

    Given the (i)th row and (j)th column, transform using index = i * rows + j where rows is the number of rows in your matrix.

提交回复
热议问题