How to add elements into Object[][] type of list, in runtime?
I have Object list, ie.
final Object[][] data ={
{"January",new Integer(150)},
{"February",new Integer(500)},
{"March",new Integer(54)},
{"April",new Integer(-50)}
};
How can I dynamicly add new elements in it, at the runtime?
Thank you in advance!
[834 byte] By [
proNicka] at [2007-11-26 18:50:02]

> How can I dynamicly add new elements in it, at the> runtime?It depends on what you mean. The array is of fixed size, so you can only replace values, not add values.Kaj
Do I have to remove 'final' for that, and then add elements?Or even better, do I have to define length of the array first?Can you be more specific about it, please?
> Do I have to remove 'final' for that, and then add
> elements?
>
No. you can't change an array's size.
You can do this
Object[][] arr = new Object[numRows][numCols];
But once you've created it, its size can't change.*
I don't know what you're doing, though, and what actual data you're putting in, but Object[][] holding rows of [String, Integer] is almost certainly a poor data structure. Think about creating a class the represents one "row" here and then create a 1D array of that class.
* Okay, you can kinda sorta effectively "change" the size of second and subsequent dimensions, since a multidimensional array is an array of arrays. I wouldn't recommend it though: int[][] arr = new int[3][2]; // a 3 x 2 rectangular array of int--it's an array of array of int, with 3 "rows", each of which is an array of int with 2 elements.
arr[0] = new int[10]; // now it's a jagged array whose first row has 10 elments instead of 2
Here we haven't changed an array's size, just replaced one of its elements, which is also an array, with a new, larger array.