Home  >  Article  >  Java  >  How to declare multidimensional arrays in java

How to declare multidimensional arrays in java

王林
王林forward
2023-04-28 16:34:071063browse

1. Concept

Create an array with array as the data type, which is an array within an array. The elements of multidimensional arrays are arrays, and they can have two-dimensional, three-dimensional, or even more-dimensional arrays.

2. Declaration

数据类型[][] 数组名称;
数据类型[] 数组名称[];
数据类型数组名称[][];

The functions of the above three syntaxes are equivalent when declaring two-dimensional arrays. In the same way, three pairs of square brackets are required when declaring a three-dimensional array. The brackets can be placed after the data type or after the array name, and so on.

3. Initialize two types Method

(1) Static initialization

int a[][] = new int[][]{{1,2,3},{1,2},{3}};

(2) Dynamic initialization

①String[][] names = new String[2][1];
②String[][] names = new String[4][];

At the same time You also need to specify how many columns there are in each row.

names[0] = new String[3];
names[1] = new String[3];
names[2] = new String[2];
names[3] = new String[1];

The above is the detailed content of How to declare multidimensional arrays in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete