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

How to declare multidimensional arrays in java

王林
Release: 2023-04-28 16:34:07
forward
1189 people have browsed it

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

数据类型[][] 数组名称;
数据类型[] 数组名称[];
数据类型数组名称[][];
Copy after login

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}};
Copy after login

(2) Dynamic initialization

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

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];
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template