# Basic use of two-dimensional arrays in Java

Charming 2022-06-24 07:41:10 阅读数:103

basicusetwo-dimensionaldimensionalarrays

@toc

# Two dimensional array

• Application scenario of two-dimensional array
• For example, we develop a Gobang game , A chessboard needs a two-dimensional array to represent . Pictured ：
1. The use of two-dimensional arrays
• Quick start case ：TwoDimensionalArray01.java
• Please use a two-dimensional array to output the following graphics
0 0 0 0 0 0
0 0 1 0 0 0
0 2 0 3 0 0
0 0 0 0 0 0
// What is a two-dimensional array ：
//1. In terms of definition and form int[][]
//2. It can be understood in this way , Each element of the original one-dimensional array is a one-dimensional array , It forms a two-dimensional array
int[][] arr = { {0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0},
{0,2, 0, 3, 0, 0},
{0, 0, 0, 0, 0, 0} };
// Output 2D graphics
for(int i = 0; i < arr.length; i++) {// Traverse each element of a two-dimensional array
// Traverse each element of a two-dimensional array ( Array )
//1. arr[i] Express The number of two-dimensional arrays i+1 Elements such as arr[0]： The first element of a two-dimensional array
//2. arr[i].length obtain Corresponding The length of each one-dimensional array
for(int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " "); // Output a one-dimensional array
}
System.out.println();// Line break
}
• give an example ： The value in the access binary array is 3 Array coordinates of // On the key concepts of two-dimensional array //(1) System.out.println(" The number of elements of a two-dimensional array =" + arr.length); //(2) Each element of a two-dimensional array is a one-dimensional array , So you need to get the value of each one-dimensional array // You need to traverse again //(3) If we want to visit (i+1) The first... Of a one-dimensional array j+1 It's worth arr[i][j]; // give an example visit 3, =》 It is the 3 The first... Of a one-dimensional array 4 It's worth arr[3-1][4-1] = arr[2][3] System.out.println(" The first 3 The first... Of a one-dimensional array 4 It's worth =" + arr[2][3]); //3 int arr[][] = new int[2][3]; // Traverse arr Array for(int i = 0; i < arr.length; i++) { for(int j = 0; j < arr[i].length; j++) {// Traverse each one-dimensional array System.out.print(arr[i][j] +" "); } System.out.println();// Line break }
• 3 The value of in a two-dimensional array corresponds to The first 3 Xing di 4 Column Value ,3 The corresponding coordinates were originally arr[3][4], But the array is from 0 At the beginning , So subtract 1, Namely arr[2][3], therefore 3 The corresponding actual coordinates are arr[2][3]
2. Usage mode 1: dynamic initialization TwoDimensionalArray02.java 1) grammar : type [][] Array name =new type [ size ][ size ] 2) such as : int a[][]=new int[2][3], Indicates that there are two one-dimensional arrays in this binary array , And there are three elements in each one-dimensional array 3) Using the demonstration
• No assignment , The default value is 0
• Initialize and assign the above array ,arr[1][1] = 8 Express stay The second element of the second one-dimensional array The value of is 8
int arr[][] = new int[2][3];
arr[1][1] = 8;
// Traverse arr Array
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++) {// Traverse each one-dimensional array
System.out.print(arr[i][j] +" ");
}
System.out.println();// Line break
}

4) The existence form of two-dimensional array in memory

## 3. Usage mode 2: dynamic initialization

TwoDimensionalArray02.java

• First of all ： type Array name [][];
• Redefine ( Open up space ) Array name = new type [ size ][ size ];
• assignment ( Have default values , such as int The type is 0)
• Using the demonstration
int arr[][]; // Declare a two-dimensional array
arr = new int[2][3]; // Open more space

## 4. Usage mode 3: dynamic initialization - Incorrect number of columns

TwoDimensionalArray03

• Code implementation ：
/*
Look at a need ： Dynamically create the following two-dimensional array , And the output
i = 0: 1
i = 1: 2 2
i = 2: 3 3 3
One has three one-dimensional arrays , The elements of each one-dimensional array are different
*/
// establish Two dimensional array , One has 3 One dimensional array , But each one-dimensional array has no data space
int[][] arr = new int[3][];
for(int i = 0; i < arr.length; i++) {// Traverse arr Each one-dimensional array
// Give each one-dimensional array space new
// If you don't give one-dimensional arrays new , that arr[i] Namely null
arr[i] = new int[i + 1];
// Traversing a one-dimensional array , And assign values to each element of a one-dimensional array
for(int j = 0; j < arr[i].length; j++) {
arr[i][j] = i + 1;// assignment
}
}
System.out.println("=====arr Elements =====");
// Traverse arr Output
for(int i = 0; i < arr.length; i++) {
// Output arr Each one-dimensional array of
for(int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();// Line break
}

## 5. Usage mode 4: initiate static

TwoDimensionalArray04.java

• Definition type Array name [][] = {{ value 1, value 2..},{ value 1, value 2..},{ value 1, value 2..}}
• Just use it Fixed access such as :
int[][] arr = {{1,1,1}, {8,8,9}, {100}};
• Defines a two-dimensional array arr
• arr There are three elements ( Every element is One dimensional array )
• The first one-dimensional array has 3 Elements , The second one-dimensional array has 3 Elements , The third one-dimensional array has 1 Elements
• Case study ： TwoDimensionalArray05.java int arr[][]={{4,6},{1,4,5,7},{-2}}; Traverse the two-dimensional array , And get and
/*
Ideas
1. Traversing a two-dimensional array , And accumulate each value to int sum
*/
int arr[][]= {{4,6},{1,4,5,7},{-2}};
int sum = 0;
for(int i = 0; i < arr.length; i++) {
// Traverse each one-dimensional array
for(int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
}
}
System.out.println("sum=" + sum);

## 6. Application case of two-dimensional array

1) Print a... Using a two-dimensional array 10 That's ok Yang hui triangle YangHui.java

int[][] yangHui = new int[12][];
for(int i = 0; i < yangHui.length; i++) {// Traverse yangHui Each element of
// Give each one-dimensional array ( That's ok ) Open space
yangHui[i] = new int[i+1];
// Give each one-dimensional array ( That's ok ) assignment
for(int j = 0; j < yangHui[i].length; j++){
// The first and last elements of each line are 1
if(j == 0 || j == yangHui[i].length - 1) {
yangHui[i][j] = 1;
} else {// The middle element
yangHui[i][j] = yangHui[i-1][j] + yangHui[i-1][j-1];
}
}
}
// Output Yang Hui triangle
for(int i = 0; i < yangHui.length; i++) {
for(int j = 0; j < yangHui[i].length; j++) {// Traverse the output line
System.out.print(yangHui[i][j] + "\t");
}
System.out.println();// Line break .
}

## 7. Details and precautions for the use of two-dimensional arrays

1) One dimensional arrays are declared in the following ways :

int[] x perhaps int x[]

2) Two dimensional arrays are declared in :

int[][] y perhaps int[] y[] perhaps int y[][]

3) Two dimensional arrays are actually composed of multiple one-dimensional arrays , The length of each one-dimensional array can be the same , It can be different . such as ： map Is a two-dimensional array

int map [][] = {{1,2},{3,4,5}}

from map[0] Is a one-dimensional array with two elements ,map[1] Is a one-dimensional array of three elements , We also call it a two-dimensional array with unequal Columns