Java 2D array

Two dimensional array

quick get 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

The code is as follows:

package ArrayList_Practice;

public class TwoDimensionalArray01 {
	public static void main(String[] args) {
		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("The matrix is as follows");
		//Export 2D graphics
		for (int i = 0; i < arr.length; i++) {//Traverses each element of a two-dimensional array
			//Traverse each element of a two-dimensional array (element)
			//1.arr[i] represents the first element of a two-dimensional array, and each element of a two-dimensional array is a one-dimensional array
			//2.arr[i].length gets the length of each one-dimensional array of two-dimensional arrays
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");
			}
				System.out.println();//Line feed
		}
	}
}

Operation diagram:

1, Related concepts

definition:

  1. Formally, int [] []

  2. Each element of the original one-dimensional array is a one-dimensional array, which constitutes a two-dimensional array

  3. 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

  4. Count the number of elements (arrays) in a two-dimensional array

    use: System.out.println("Number of elements of two-dimensional array:"+ arr.length); Make statistics
    

​ 5. If we want to access the j + 1st value of the (I + 1st) one-dimensional array

System.out.println(arr[2][3]);

2, Dynamic initialization

2.1 various methods of dynamic initialization

Method 1

Syntax: type [] [] array name = new type [size] [size]

  • The first [] indicates that there are several one-dimensional arrays
  • The second [] indicates that there are several elements in this one-dimensional array
 such as: int a[][]=new int[2][3]

Method 2

  1. Declare first: type array name [] [];

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

  3. Assignment (with default value, such as 0 for int type)

package TwoDimenSionalArray;

public class TowDimensionalArray01 {
	public static void main(String[] args) {
//		int arr[][] = new int[2][3];
		
		int arr[][];
		arr = new int[2][3];
		//Traversal of arr array
		arr[1][1] = 8;
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}
	}
}

Method 3

Dynamic initialization - uncertain number of columns

/*
			Look at a requirement: dynamically create the following two-dimensional array and output it
			i = 0: 1
			i = 1: 2 2
			i = 2: 3 3 3 One has three one-dimensional arrays, and the elements of each one-dimensional array are different
		*/
		int arr[][] = new int[3][];//The number of columns is uncertain. Here, only the space of one-dimensional array is determined. Unlike c language, c language can only have an uncertain number of rows
		//Traverse each one-dimensional array of arr
		for (int i = 0; i < arr.length; i++) {
			//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;
			}
		}
		
		//ergodic
		for (int i = 0; i < arr.length; i++) {
		
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j]+" ");
			}
			System.out.println();
		}

Personal understanding:

  1. int arr[][] = new int[3][]; It only declares the number of one-dimensional arrays contained in the two-dimensional array, but each element is a NULL pointer because it does not open up space
  2. Opening up space is to let the arr array point to a space that can store i+1 ints, so that the one-dimensional array can have an address

Operation effect:

Method 4

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

such as

int[][] arr = {{1,1,1}, {8,8,9},{100}}
1.Defines a two-dimensional array arr
2.arr There are three elements in the array, the second one-dimensional array has three elements, and the third one-dimensional prime group has one element
package TwoDimenSionalArray;

public class TwoDimensionalArray02 {
	public static void main (String [] args) {
		//Traverse the array and get its sum
		int arr[][] = {{4,6},{1,4,5,7},{-2}};
		int sum = 0;
		System.out.println("====arr Array element====");
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				sum += arr[i][j];
				System.out.print(arr[i][j]+" ");
			}
			System.out.println();
		}
		System.out.println("Sum of elements:"+sum);
		
	}
}

Operation results:

====arr Array element====
4 6 
1 4 5 7 
-2 
Sum of elements:25

2.2 form of two-dimensional array in memory

be careful:

  1. In java, if there is no assignment to the array, it has the default value, int is 0, which is different from the c language

2.3 precautions and use details

  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

3, Practice

3.1 Yanghui triangle

package TwoDimenSionalArray;

public class YangHui {
	public static void main(String[] args) {
	/*	
	 * Yang Hui triangle
	 *  Print a 10 Line Yang huisan using a two-dimensional array
	 *	1
	 *	1 1
	 *	1 2 1
	 *	1 3 3 1
	 *	1 4 6 4 1
	 *	1 5 10 10 5 1
     *
	 *	law
	 *	1.The first line has 1 element and the nth line has n elements
	 *	2. The first and last elements of each line are 1
	 *	3. Starting from the third line, values for elements other than the first and last elements
	*/
		int yangHui[][] = new int[12][];
		System.out.println("===Yang Hui triangle is as follows===");
		for (int i = 0; i < yangHui.length; i++) {
			//Make room for each one-dimensional array (row),
			yangHui[i] = new int[i+1];
			//Assign a value to each one-dimensional array (row)
			for (int j = 0; j < yangHui[i].length; j++) {
				//The first and last are assigned a value of 1
				if( j == 0 || j == yangHui[i].length -1) {
					yangHui[i][j] = 1;
				}else {
					//If it is neither the first nor the last, add up the values of column j in row i-1 and column j-1 in row i-1
					yangHui[i][j] = yangHui[i-1][j-1] + yangHui[i-1][j];
				}
			}
			
		}
		for (int i = 0; i < yangHui.length; i++) {
			for (int j = 0; j < yangHui[i].length; j++) {
				System.out.print(yangHui[i][j]+"  ");
			}
				System.out.println();
		}
		
	}
}

Operation effect:

3.2 objective questions

3.3 insertion

Title: it is known that an ascending sequence requires the insertion of an element, and the array is still in ascending order

package TwoArrayHomeWroks;

import java.util.Iterator;

public class Homework04 {
	public static void main(String[] args) {
		//Known as an ascending array, it is required to insert an element. The array is in ascending order

		int[] array = { 10, 12, 45, 90 };
		int[] newArray = new int[array.length + 1];
		int key = 0;
		int temp = 0;
		int index = 0;
		
		System.out.println("====Before insertion====");
		//Before insertion
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] +" ");
		}
		
		//Determine the subscript to insert
		for (int i = 0; i < array.length; i++) {
			if( array[i]>= key) {
				index = i;
				break;
			}
			
			if(i == array.length-1 && index == 0){
				index = array.length;
			}
		}
		
		//Insert using two arrays
		for (int i = 0, j = 0; i < newArray.length; i++) {
			if( i== index) {
				newArray[i] = key;
				
			}else {
				newArray[i] = array[j];
				j++;
			}
		}
		
		System.out.println("\n====After insertion====");
		for (int i = 0; i < newArray.length; i++) {
			System.out.print(newArray[i]+" ");
		}
		
	}
}

3.4 maximum and minimum

Title:

  • Randomly generate 10 integers (1 ~ 100) and save them to the array, print them in reverse order, find the average value, the subscript of the maximum value and the subscript of the minimum value, and find out whether there are 8
package TwoArrayHomeWroks;

import java.util.Random;


public class Homework05 {
		public static void main(String[] args) {
			double average = 0;
			int sum = 0;
			int max = 0;
			int min = 0;
			int key = 8;
			int arr[] = new int[10];
			boolean flag = false;
			
			//Positive sequence printing
			System.out.println("====Positive sequence printing====");
			Random random = new Random();
			for (int i = 0; i < arr.length; i++) {
				int temp = random.nextInt(100);
				arr[i] = temp;
				//Print
				System.out.print(arr[i] + " ");
				sum += temp;
				if(temp == key) {
					flag = true;
				}
			}
			average = sum*1.0/ arr.length;
			
			//Reverse order printing
			System.out.println("\n====Reverse order printing====");
			for(int i = arr.length - 1 ; i >= 0 ; i--) {
				System.out.print(arr[i] + " ");
			}
			
			//Find the subscripts for the minimum and maximum values
			for (int i = 0; i < arr.length; i++) {
				if(arr[max] < arr[i]){
					max = i;
				}
				if(arr[min] > arr[i]) {
					min = i;
				}
			}
			
			
			System.out.println("\n The average value is:" + average);
			System.out.println("Maximum subscript" + max);
			System.out.println("Minimum subscript" + min);
			System.out.print("Are there 8:");
			if(flag) {
				System.out.print("yes");
			}else {
				System.out.println("no");
			}
		}
}

Keywords: Java array

Added by mania on Sun, 09 Jan 2022 16:21:29 +0200