I'm not sure how to initialize such an array or store information.
Like this for instance:
List<List<Integer>> twoDim = new ArrayList<List<Integer>>();
twoDim.add(Arrays.asList(0, 1, 0, 1, 0));
twoDim.add(Arrays.asList(0, 1, 1, 0, 1));
twoDim.add(Arrays.asList(0, 0, 0, 1, 0));
or like this if you prefer:
List<List<Integer>> twoDim = new ArrayList<List<Integer>>() {{
add(Arrays.asList(0, 1, 0, 1, 0));
add(Arrays.asList(0, 1, 1, 0, 1));
add(Arrays.asList(0, 0, 0, 1, 0));
}};
To insert a new row, you do
twoDim.add(new ArrayList<Integer>());
and to append another element on a specific row
you do
twoDim.get(row).add(someValue);
Here is a more complete example:
import java.util.*;
public class Test {
public static void main(String[] args) {
List<List<Integer>> twoDim = new ArrayList<List<Integer>>();
String[] inputLines = { "0 1 0 1 0", "0 1 1 0 1", "0 0 0 1 0" };
for (String line : inputLines) {
List<Integer> row = new ArrayList<Integer>();
Scanner s = new Scanner(line);
while (s.hasNextInt())
row.add(s.nextInt());
twoDim.add(row);
}
}
}