Home > Java > javaTutorial > body text

How to initialize a two-dimensional array in Java

王林
Release: 2023-05-15 15:07:06
forward
4894 people have browsed it

1. Two-dimensional array description

An array is a container used to store data. Now the types stored in the array are no longer int, double.., but the stored array.

The elements in the array are still arrays. We call it an array within an array, which is also a two-dimensional array. One more layer of dimensionality.

Simply speaking, a two-dimensional array is an array whose elements are one-dimensional arrays.

2. Initialization method

(1) Use braces to assign values ​​directly, suitable for situations where the array elements have been determined

(2) Given The size of the two-dimensional array

(3) The length of the second dimension of the array can be changed, but remains unchanged

3. Initialization instance

public class Note04_ArrayText2 {
public static void main(String[] args) {
int[][] array = new int[3][3];
System.out.println(array);//地址
System.out.println(array[1]);//地址
System.out.println(array[1][1]);//0
int[][] array2 = new int[3][];
System.out.println(array2);//地址
System.out.println(array2[1]);//null
//System.out.println(array2[1][1]);//报错
}
}
Copy after login

For a For newly used tools, we will perform preliminary initialization of the tools in order to add some configurations for use. After learning about one-dimensional arrays, two-dimensional arrays are one-dimensional arrays with one layer added. In terms of initialization, there are three methods for two-dimensional arrays. I believe many people have only mastered one of them.

The above is the detailed content of How to initialize a two-dimensional array in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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