IT Band of Brothers Java syntax to initialize an array of tutorials multidimensional array of two-dimensional arrays

Initialized with an array of two-dimensional array initialization is similar, the same can be used to initialize static or dynamic initialization.

1) static initialization

Static initialization format is as follows:

Type name = new Array array element [] [] {new types of array elements [] {element 1, element 2, ...}, new types of array elements [] {element 1, element 2, ...} , ...};

Not difficult to see, the static two-dimensional array is initialized with the static initialization like a one-dimensional array, just a one-dimensional array, each element is initialized to a static array again.

The following code demonstrates how to initialize a static two-dimensional array:

int[][] arr;

arr = new int[][]{new int[]{1,2,3},new int[]{4,5,6}};

Course also be used to simplify the initialization syntax to initialize static two-digit groups, as follows:

int[][] arr = {{1,2,3},{4,5,6}};

However, this syntax requirements must be initialized in a statement that the array.

2) Dynamic initialization

Dynamic initialization the following format:

Type name = new Array array element [length of the array] [length of the array];

Course also be used in the following manner:

Type name = new Array array element [length of the array] [];

Array name [array index] = new array element types [length of the array];

The following code demonstrates how to initialize a dynamic two-dimensional array:

int[][] arr;

arr = new int[10][10];

int[][] arr2;

arr = new int[10][];

arr[0] = new int[10];

It can be obtained by the above explanation one conclusion: double-digit group is a one-dimensional array, which array element is one-dimensional array; a three-dimensional array is one-dimensional array, which array element is a two-dimensional array ...... From this perspective, Java language no multidimensional arrays.

Guess you like

Origin www.cnblogs.com/itxdl/p/11267826.html