VSB VSB - 4 months ago 7
Java Question

Can rows have different column count?

Is it possible that I can have arrays which its rows have different column count?

Answer

Yes, that is possible, here is example of iterating through them

public static void main(String[] args) {
        int[][] numbers = new int[][] { { 1, 2, 3 }, { 1, 2, 3, 4 }, { 1, 2, 3, 4, 5 } };
        for(int i = 0; i<numbers.length;i++) {
            for(int j = 0; j < numbers[i].length; j++) {
                System.out.print(numbers[i][j]);
            }
            System.out.println("");
        }
    }

output:

123
1234
12345
Comments