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]
# 1
> 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
kajbja at 2007-7-9 6:24:03 > top of Java-index,Java Essentials,New To Java...
# 2
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?
proNicka at 2007-7-9 6:24:03 > top of Java-index,Java Essentials,New To Java...
# 3

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

jverda at 2007-7-9 6:24:03 > top of Java-index,Java Essentials,New To Java...
# 4
When you add data like you have in the first post you don't need to give a length. Can you not use another collection which does not have a fixed size after creation?Why not use a HashMap, with key value pairs?
kikemellya at 2007-7-9 6:24:03 > top of Java-index,Java Essentials,New To Java...
# 5
Array tutorial: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.htmlCollections tutorial. A List might is more appropriate if the size isn't known at creation time, as Lists can grow and shrink on the fly: http://java.sun.com/docs/books/tutorial/collections/
jverda at 2007-7-9 6:24:04 > top of Java-index,Java Essentials,New To Java...
# 6
Thank you on your advices and examples!I need Object[][], because I want to use it building AbstractTableModel. If you have more suggestions, please reply.
proNicka at 2007-7-9 6:24:04 > top of Java-index,Java Essentials,New To Java...
# 7
I don't do GUIs, and I don't know anything about what you're trying to do or how you're doing it, so I'm done.
jverda at 2007-7-9 6:24:04 > top of Java-index,Java Essentials,New To Java...
# 8
> I need Object[][], because I want to use it building> AbstractTableModel. Use DefaultTableModel instead. It has a constructor which takes a Vector of Vectors.Kaj
kajbja at 2007-7-9 6:24:04 > top of Java-index,Java Essentials,New To Java...