This documentation is archived and is not being maintained.

Rectangular Arrays

Visual Studio 2005

In addition to jagged arrays, Visual J# also supports rectangular arrays. The element type and shape of an array — including the number of dimensions it has — are part of its type. However, the size of the array, as represented by the length of each of its dimensions, is not part of an array's type.

This split is made clear in the Visual J# language syntax, as the length of each dimension is specified in the array creation expression rather than in the array type. For instance, the declaration int[,,] arr = new int[1, 1, 2]; is an array type of int[,,] and an array creation expression of new int[1, 1, 2].

It is also possible to mix rectangular and jagged arrays. For example, int [,][] mixedArr = new int[,][] {{{1,2}}, {{2,3,4}}}; is a mixed two-dimensional rectangular array of jagged arrays. The array is a rectangular array of dimensions [2,1], each element of which is a single dimensional integer array. The length of the array at mixedArr[0, 0] is 2, and the length of the array at mixedArr[1, 0] is 3.

The following samples show how rectangular arrays can be created in Visual J#.

// vjc_rect_array.jsl
public class MyClass
{
    public static void main(String [] args)
    {
        // 2-D rectangular array; explicit creation.
        int[,] arr = new int[2, 2];
        arr[0, 0] = 1;
        arr[0, 1] = 2;
        arr[1, 0] = 3;
        arr[1, 1] = 4;

        // 2-D rectangular array;
        // Explicit creation with initializer list.
        int[,] arr2 = new int[,] {{1, 2}, {3, 4}};

        // 2-D rectangular array;
        // Shorthand: creation with initializer list.
        int[,] arr3 = {{1, 2}, {3, 4}};

        // 3-D rectangular array; explicit creation.
        int[,,] arr4 = new int[1, 1, 2];
        arr4[0, 0, 0] = 1;
        arr4[0, 0, 1] = 2;

        // 3-D rectangular array;
        // Explicit creation with initializer list.
        int[,,] arr5 = new int [,,] 
            {{{1,2,3},{1,2,3}},{{1,2,3},{1,2,3}}};

        // Mix rectangle & jagged arrays; explicit creation using
        // new. Creates the same array as the above example.
        int [,][] mixedArr = new int[,][] {{{1,2}}, {{2,3,4}}};
        
        // Mixed array dynamic creation
        int [,][] mixedArr2 = new int[2,1][];
        mixedArr2[0,0] = new int[]{1,2};
        mixedArr2[1,0] = new int[]{2,3,4};
    }
}
Show: