Home  >  Article  >  Java  >  How to initialize a two-dimensional array in Java

How to initialize a two-dimensional array in Java

王林
王林forward
2023-05-15 15:07:064895browse

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]);//报错
}
}

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!

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