Java basic programming (array)

Keywords: Java Back-end

1, Overview of arrays

1. Understanding of arrays:

                 An array is a collection of multiple data of the same type arranged in a certain order, named with a name, and managed uniformly by numbering.

2. Array related concepts:

          > Array name

          > element

          > Corner mark, subscript, index

          > Len gt h of array: the number of elements, and these data are managed uniformly by numbering.

3. Array features:

                1) Arrays are ordered

                 2) Array is a variable of reference data type. The elements of an array can be either basic data types or references

                       data type

                 3) Creating an array object opens up a whole contiguous space in memory

                 4) Once the length of the array is determined, it cannot be modified.

4. Array classification:

                 ① According to dimension: one-dimensional array, two-dimensional array...

                 ② According to the type of array elements: array of basic data type elements and array of reference data type elements

2: One dimensional array

1: Declaration and initialization of one-dimensional array         

        int[] ids;//statement

		//1.1 static initialization: array initialization and array element assignment are performed simultaneously
		ids = new int[]{1001,1002,1003,1004};

		//1.2 dynamic initialization: array initialization and array element assignment are performed separately
		String[] names = new String[5];

        //It is also the correct way to write:
		int[] arr4 = {1,2,3,4,5};//Type inference

  Declaration method of one-dimensional array: type var [] or type[] var;               

   int a[];

   int[] a1;

   double b[];

   String[] c;

Dynamic initialization: array declaration and allocation of space for array elements are performed separately from assignment

int[] arr = new int[3];
arr[0] = 3;
arr[1] = 9;
arr[2] = 8;


String names[];
names = new String[3];
names[0] = "Qian Xuesen ";
names[1] = ""Deng Jiaxian";
names[2] = ""Yuan Longping";

Static initialization: allocate space and assign values to array elements while defining the array.

int arr[] = new int[]{ 3, 9, 8};
or
int[] arr = {3,9,8};

String names[] = {
""Li Siguang",""Mao Yisheng","Hua Luogeng
}

                 Summary: once the array is initialized, its length is determined.

2. How to call the element at the specified position of the array: call by corner marker.

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

3: How to get the length of an array

                 Each array has an attribute length indicating its length. For example, a.length indicates the length of array a (number of elements)

//3. How to get the length of the array.
		//Attribute: length
		System.out.println(names.length);//5
		System.out.println(ids.length);

4. How to traverse an array

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

5: Default initialization value for array elements

                > Array element is an integer: 0

                > Array elements are floating point: 0.0

                > Array elements are of type char: 0 or '\ u0000', not '0'

                > Array elements are boolean: false

                > Array element is a reference data type: null

  6: Memory parsing of arrays

  7: Memory parsing of one-dimensional array

 

3: Two dimensional array

  1. Understand:

                 For the understanding of two-dimensional array, we can see that one-dimensional array array1 exists as an element of another one-dimensional array array2.

                 In fact, from the perspective of the underlying operation mechanism of the array, there is no multidimensional array.

2. Use of two-dimensional array:

                 ① Declaration and initialization of two-dimensional array

                 ② How to call an element at a specified position of an array

                 ③ How to get the length of an array

                 ④ How to traverse an array

                 ⑤ Default initialization value of array elements: see ArrayTest3.java

                 ⑥ Memory analysis of array: see ArrayTest3.java

//1. Declaration and initialization of two-dimensional array
		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][];
		//Wrong situation 
//		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}};
		
		//It is also the correct 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}};
		
		//2. How to call the element at the specified position of the array
		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]);
		
		//3. Get the length of the array
		System.out.println(arr4.length);//3
		System.out.println(arr4[0].length);//3
		System.out.println(arr4[1].length);//4
		
		//4. How to traverse a two-dimensional array
		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();
/*
 * Use of 2D arrays:
 * 	Regulation: the two-dimensional array is divided into the elements of the outer array and the elements of the inner array
 * 		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 initialization method 1: for example: int[][] arr = new int[4][3];
 *      The initialization value of the outer element is: address value
 *      The initialization value of the inner element is: the same as that of the one-dimensional array
 *      
 *   For initialization mode 2: for example: int[][] arr = new int[4] [];
 *   	The initialization value of the outer element is null
 *      The initialization value of the inner element is: it cannot be called, otherwise an error will be reported.
 *   
 *   ⑥ Memory parsing of arrays 
 * 
 */
public class ArrayTest3 {
	public static void main(String[] args) {
		
		int[][] arr = new int[4][3];
		System.out.println(arr[0]);//[I@15db9742 
		System.out.println(arr[0][0]);//0
		
//		System.out.println(arr);//[[I@6d06d69c
		
		System.out.println("*****************");
		float[][] arr1 = new float[4][3];
		System.out.println(arr1[0]);//Address value
		System.out.println(arr1[0][0]);//0.0
		
		System.out.println("*****************");
		
		String[][] arr2 = new String[4][2];
		System.out.println(arr2[1]);//Address value
		System.out.println(arr2[1][1]);//null
		
		System.out.println("*****************");
		double[][] arr3 = new double[4][];
		System.out.println(arr3[1]);//null
//		System.out.println(arr3[1][0]);// report errors
		
	}
}

4: Algorithms commonly used in arrays

1. Assignment of array elements (Yang Hui triangle, loop number, etc.)

2. Find the maximum, minimum, average, sum, etc. of the elements in the numerical array

3. Array copy, inversion and search (linear search, dichotomy search)

4. Sorting algorithm of array elements

Posted by kcomer on Fri, 29 Oct 2021 20:16:27 -0700