I want to create a multi-dimension ArrayList - I don't know the size, it should be decided at runtime.
how will I do that and how to access it?
It will be array of integers.
Use
List<List<Integer>>
List by itself isn't multidimensional -- but you can use it to store Lists, which can then store Integers, in effect acting as a multidimensional array. You can then access elements as:
// Get the element at index x,y
int element = list[x][y];
To populate the list with initial elements, with dimensions x and y:
for (int i=0; i<x; i++)
{
// Have to create the inner list for each index, or it'll be null
list.Add(new List<Integer>());
for (int j=0; j<y; j++)
{
list[i].Add(someValue); // where someValue is whatever starting value you want
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With