Java self-learning day7-1-D array, 2-D array summary

An overview of arrays

1. Understanding arrays: Array is a set of data of the same type arranged in a certain order, named by a name, and managed by numbering.
2. Concepts related to arrays:

Array name
element
Corner, Subscript, Index
Array length: number of elements

3. The characteristics of arrays:

1. Arrays are ordered
2. Arrays are variables that refer to data types. The elements of an array can be either basic data types or reference data types
3. Creating an array object creates a whole contiguous block of space in memory
4. Once the length of the array is determined, it cannot be modified.

4. Classification of arrays:
(1) Illumination dimension: one-dimensional array, two-dimensional array,...
(2) Refer to the types of array elements: arrays of basic data type elements, arrays of referenced data type elements

5. Data structure:
1. Logical relationship between data and data: set, one-to-one, one-to-many, many-to-many
2. Storage structure of data:

Linear Table: Sequential Table (e.g., Array), Chain Table, Stack, Queue
Tree structure: binary tree
Graphic structure

6. Algorithms
Sorting algorithm:
Search algorithm:

One-dimensional array

1. Declarations and initialization of one-dimensional arrays

The right way:
	int num;//statement
		num = 10;//Initialization
		int id = 1001;//Declaration+Initialization
		
		int[] ids;//statement
		//1.1 Static Initialization: Initialization of arrays and assignment of array elements occur simultaneously
		ids = new int[]{1001,1002,1003,1004};
		//1.2 Dynamic Initialization: Array initialization and assignment of array elements are done separately
		String[] names = new String[5];

	int[] arr4 = {1,2,3,4,5};//Type Inference

Wrong way:
//		int[] arr1 = new int[];
//		int[5] arr2 = new int[5];
//		int[] arr3 = new int[3]{1,2,3};

2.Reference to one-dimensional array elements: called by means of a corner.

	//Corner of the array (or index starts at 0 and ends at length-1 of the array).
	names[0] = "Wang Ming";
	names[1] = "Wang He";
	names[2] = "placed under house arrest";
	names[3] = "Sun Julong";
	names[4] = "Wang Hongzhi";//charAt(0)

3. Array properties: length

System.out.println(names.length);//5
System.out.println(ids.length);

Explain:
Once the array is initialized, its length is determined. arr.length
Once the length of the array is determined, it cannot be modified.
4. Traversal of one-dimensional arrays

for(int i = 0;i < names.length;i++){
	System.out.println(names[i]);
}

5. Default initialization values for one-dimensional array elements

Array elements are integers: 0
Array elements are floating point: 0.0
Array elements are char type: 0 or'\u0000', not'0'
Array elements are boolean: false
Array elements are reference data types: null

6. Memory resolution of one-dimensional arrays

2-D Array

1. How do you understand two-dimensional arrays?
Array is of reference data type
An element of an array can also be a reference data type
If the element of a one-dimensional array A is also a one-dimensional array type, the array A is called a two-dimensional array.

2. Declarations and initialization of two-dimensional arrays

The right way:

	int[] arr = new int[]{1,2,3};//One-dimensional array
		//initiate static
		int[][] arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}};
		//Dynamic Initialization 1
		String[][] arr2 = new String[3][2];
		//Dynamic Initialization 2
		String[][] arr3 = new String[3][];
	//It's also the right way to write:
		int[] arr4[] = new int[][]{{1,2,3},{4,5,9,10},{6,7,8}};
		int[] arr5[] = {{1,2,3},{4,5},{6,7,8}};//Type Inference
 Wrong way:
//		String[][] arr4 = new String[][4];
//		String[4][3] arr5 = new String[][];
//		int[][] arr6 = new int[4][3]{{1,2,3},{4,5},{6,7,8}};

3. How to invoke a two-dimensional array element

	System.out.println(arr1[0][1]);//2
	System.out.println(arr2[1][1]);//null
	
	arr3[1] = new String[4];
	System.out.println(arr3[1][0]);
System.out.println(arr3[0]);//

4. Properties of a two-dimensional array

System.out.println(arr4.length);//3
	System.out.println(arr4[0].length);//3
	System.out.println(arr4[1].length);//4

5. Traversing through two-dimensional array elements

for(int i = 0;i < arr4.length;i++){
		
		for(int j = 0;j < arr4[i].length;j++){
			System.out.print(arr4[i][j] + "  ");
		}
		System.out.println();
}

6. Default initialization values for two-dimensional array elements

Specification: Two-dimensional arrays are divided into elements of outer arrays and elements of inner arrays
int[][] arr = new int[4][3];
Outer elements: arr[0],arr[1], etc.
Inner elements: arr[0][0],arr[1][2], etc.
Default initialization value for array elements
For the first method of initialization: for example, int[][] arr = new int[4][3];
The initialization value of the outer element is the address value
Initialization value of the inner element is the same as one-dimensional array initialization
For initialization method two: for example, int[][] arr = new int[4][];
The initialization value of the outer element is: null
The initialization value of the inner element is: cannot be called, otherwise an error is reported.

7. Memory structure of two-dimensional arrays

Note: Detailed Memory Resolution Blog Please check it yourself (click Jump)

Added by Robin M on Fri, 04 Mar 2022 01:26:04 +0200