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
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.