I have a constant 2d int array which is declared as:
int blocked[][] = new int[][] { { 0, 4 }, { 2, 2 }, { 3, 1 }, { 3, 3 } };
However, what I would like to do is to generate the exact array dynamically. So I created an arraylist where I add each integer separately, 0,4,2,3...N. Then I use for loop to go through to arraylist and create a 2d array. But somehow I just can't get it to work. I can't figure out where I'm doing wrong.
ArrayList<Integer> blocklist = new ArrayList<Integer>();
int blocked[][];
blocklist.add(0);
blocklist.add(4);
blocklist.add(2);
blocklist.add(2);
blocklist.add(3);
blocklist.add(1);
blocklist.add(3);
blocklist.add(3);
blocked = new int[blocklist.size()][2];
for(int i=0; i+2 < blocklist.size(); i++){
blocked[i][0] = blocklist.get(i);
blocked[i][1] = blocklist.get(i+1);
}
for(int i=0;i<blocked.length;++i){
System.out.print(blocked[i][0]);
}
When I do Arrays.deepToString(blocked);
I get [[0, 0], [4, 4], [2, 2], [2, 2], [3, 3], [1, 1], [3, 3], [3, 3]]
but it should be [[0, 4], [2, 2], [3, 1], [3, 3]]