I need a dynamic two dimensional array in Java. I thought I might not be the only one, so I searched with regular search engines and the code searches. Surprisingly I did not find any free implementation. I thought this would be a such common case, that the JRE or at least the
Jakarta Commons Collections might contain it. No, they do not.
It seems the rest of the world builds their own implementation or uses the
javax.swing.table.TableModel and
javax.swing.table.DefaultTableModel. The public API of the latter gets near to what I want, but it’s not exactly what I was looking for.
I thought about something like this:
interface TwoDimensionalArray<T>
{
T set(int x, int y, T value);
boolean add(int x, int y, T value);
T get(int x, int y);
Iterator<Iterable<T>> iterator();
boolean isEmpty();
/** returning the overall element count */
int size();
}