If you really want to initialize a 2D jagged array you could do something like this:
public static void main( String [] args){
int [][] jaggedArray = new int [15][];
for (int i=0; i<15; i++){
jaggedArray[i] = new int[i+1];
}
However, in the question it looks like you're checking at runtime if the characters in the string are numbers and adding them dynamically. The problem with ArrayLists vs Arrays is that the former lets you change the size dynamically whereas the latter doesn't. So you either have to:
- Know the static size ahead of time and intialize it the way above
- Go through the string and determine the static size as others have said and then initialize it
- Otherwise, you need to use a data structure that allows for dynamic resizing
Also, I'm not sure of the application, but if you're guaranteed that this thing won't be bigger than 15x15, I wouldn't worry too much about the tiny bit of over-allocation.