CS 123 - Multidimensional Arrays

Here is an illustration of the memory map for the following code fragment.

    int[][] matrix1 = { {1, 2, 3}, {4, 5, 6} };
    int[][] matrix2 = { {1, 2}, {3, 4, 5}, {6, 7, 8, 9} };

    int[][] matrix3;
    int[][] matrix4;

    matrix3 = new int[5][];
    matrix3[0] = matrix1[0];
    matrix3[1] = matrix2[0];
    matrix3[2] = new int[10];
    matrix3[3] = matrix2[2];
    matrix3[4] = matrix1[1];

    matrix4 = new int[4][];
    matrix4[0] = matrix1[0];
    matrix4[1] = matrix1[0];
    matrix4[2] = matrix1[0];
    matrix4[3] = matrix1[0];

    matrix1[0][1] = 999;


Return to the CS 123 lecture page.


compliments and criticisms