Java learning notes: two-dimensional array

Posted by Cornelia on Thu, 02 Sep 2021 09:43:23 +0200

catalogue

What is a two-dimensional array?

Quick start:

Use of 2D arrays:

Usage 1: dynamic initialization

Syntax:  

Case presentation:

Usage 2: dynamic initialization

Syntax:

Usage 3: dynamic initialization - uncertain number of columns

Case demonstration

Requirements: use dynamic initialization to create the following two-dimensional array and output it.

  The code is as follows:

Usage 4: static initialization

Syntax:

Analysis code:

  Case presentation:

Details and precautions of using two-dimensional array

What is a two-dimensional array?

one   In terms of definition and form

int[][]

two   Each element of a one-dimensional array is a one-dimensional array, which constitutes a two-dimensional array

3. Each element of the two-dimensional array is a one-dimensional array, so if you need to get the value of each one-dimensional array, you need to traverse again

Quick start:

        /*
		Please output the following graphics with a two-dimensional array
			0 0 0 0 0 0
			0 0 1 0 0 0	
			0 2 0 3 0 0
			0 0 0 0 0 0
		 */
public class TwoDimensionalArray01 { 

	public static void main(String[] args) {
		
		//What is a two-dimensional array: 
		//1. From the definition form, int [] []
		//2. It can be understood that each element of the original one-dimensional array is a one-dimensional array, which constitutes 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} };

		
		System.out.println("Number of elements of two-dimensional array=" + arr.length);
        //Key concepts of two-dimensional array
		//(1) Each element of a two-dimensional array is a one-dimensional array, so if you need to get the value of each one-dimensional array
		//    You need to traverse again
	
    	//(2) If we want to access the j + 1st value arr[i][j] of the (I + 1st) one-dimensional array;
		//    For example, access 3, = "which is the fourth value arr[2][3] of the third one-dimensional array
		System.out.println("The fourth value of the third one-dimensional array=" + arr[2][3]); //3


		//Export 2D graphics
		for(int i = 0; i < arr.length; i++) {//Traverses each element of a two-dimensional array
			
			for(int j = 0; j < arr[i].length; j++) {
            //1. arr[i] represents the i+1 element of the two-dimensional array, such as arr[0]: the first element of the two-dimensional array
		
        	//2. arr[i].length obtains the length of each corresponding one-dimensional array 
				System.out.print(arr[i][j] + " "); //Output a one-dimensional array
			}
			System.out.println();//Line feed
		}
	}
}

Use of 2D arrays:

Usage 1: dynamic initialization

Syntax:  

Type [] []   Array name = new type [size] [size]

For example:

 int a[][]=new int[2][3]

2 here means that there are two one-dimensional arrays. 3 means that there are 3 elements in each one-dimensional array

Case presentation:

public class TwoDimensionalArray02 {
public static void main(String[] args) {
    
    int arr[][]; //Declare a two-dimensional array
    arr = new int[2][3];//Reopen space
    arr[1][1] = 8;

        for(int i = 0; i < arr.length; i++) {//Traversal of arr array
            for(int j = 0; j < arr[i].length; j++) {//Traverse each one-dimensional array 
                System.out.print(arr[i][j] +" ");
                }
            System.out.println();//Line feed
            }
}
}

Usage 2: dynamic initialization

Syntax:

Declare first: type array name [] [];         

Redefine (open up space) array name = new type [size] [size] assignment

(there are default values, such as 0 for int type)

Usage 3: dynamic initialization - uncertain number of columns

Case demonstration

Requirements: use dynamic initialization to create the following two-dimensional array and output it.

  The code is as follows:

public class TwoDimensionalArray03 { 

	public static void main(String[] args) {
		
		//Create two-dimensional arrays. One has three one-dimensional arrays, 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 the one-dimensional array new is not given, then arr[i] is null
			arr[i] = new int[i + 1]; 

			//Traverse a one-dimensional array and assign a value to each element of the one-dimensional array
			for(int j = 0;  j < arr[i].length; j++) {
				arr[i][j] = i + 1;//assignment
			}

		}

		
		for(int i = 0; i < arr.length; i++) {//Traversing the arr output. / / traversing the arr output
			
			for(int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");//Output each one-dimensional array of arr
			}
			System.out.println();//Line feed
		}
		

	}
}

Usage 4: static initialization

Syntax:

Definition type array name [] []     = {{ Value 1, value 2..}, {value 1, value 2..}, {value 1, value 2..}}

For example:

int[][] arr = {{1,1,1}, {2,2}, {3}};

Analysis code:

one   A two-dimensional array arr is defined
2. arr has three elements (each element is a one-dimensional array)
3. The first one-dimensional array has 3 elements and the second one-dimensional array has 2 elements   The third one-dimensional array has 1 element

  Case presentation:

/*
int arr[][]={{4,6},{1,4,5,7},{-2}}; Traverse the two-dimensional array and get and
*/
public class TwoDimensionalArray05 {

    public static void main(String[] args) {


    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++) { //Traversing a two-dimensional array

            sum += arr[i][j];//Accumulate values to int sum
            }
        }
System.out.println(sum);
}
}

Details and precautions of using two-dimensional array

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

 int[] x    perhaps int x[]

(2)      Two dimensional arrays are declared in the following ways:
 

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

(3)      A two-dimensional array is actually composed of multiple one-dimensional arrays. The length of each one-dimensional array can be the same or different. For example: map [] []   Is a two-dimensional array

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


map[0] is a one-dimensional array with two elements, and map[1] is a one-dimensional array with three elements. We also call it a two-dimensional array with unequal columns

 

Topics: Java Eclipse Algorithm