Chapter III array

Preface: array

1, One dimensional array

1. Understanding of arrays:

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

2. Array related concepts:

Array name
element
Corner mark, subscript, index
Length of array: number of elements

3. Array features:

  • 1) Arrays are ordered
  • 2) Array is a variable of reference data type. The element of an array can be either a basic data type or a reference 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

5. Use of one-dimensional array

  • ① Declaration and initialization of one-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

6. Default initialization value of array element:

      Array element is an integer: 0
	  Array elements are floating point: 0.0
	  Array elements are char Type: 0 or'\u0000',Not'0'
	  Array elements are boolean Type: false
      Array elements are reference data types: null

7. Memory parsing of array:

2, One dimensional array code example:

public static void main(String[] args) {
		
		//1. Declaration and initialization of one-dimensional array
		int num;//statement
		num = 10;//initialization
		int id = 1001;//Declaration + initialization
		
		int[] ids;//statement
		//1.1 static initialization: the initialization of array and the assignment of array elements are carried out at the same time
		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];
		
		//Incorrect wording:
//		int[] arr1 = new int[];
//		int[5] arr2 = new int[5];
//		int[] arr3 = new int[3]{1,2,3};
		
		//It is also the correct way to write:
		int[] arr4 = {1,2,3,4,5};//Type inference
		
		//Summary: once the array is initialized, its length is determined.
		
		//2. How to call the element at the specified position of the array: call it 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 the array.
		//Attribute: length
		System.out.println(names.length);//5
		System.out.println(ids.length);
		
		//4. How to traverse an array
		/*System.out.println(names[0]);
		System.out.println(names[1]);
		System.out.println(names[2]);
		System.out.println(names[3]);
		System.out.println(names[4]);*/
		
		for(int i = 0;i < names.length;i++){
			System.out.println(names[i]);
		}
		
		
	}
public static void main(String[] args) {
		//5. Default initialization value of array element
		int[] arr = new int[4];
		for(int i = 0;i < arr.length;i++){
			System.out.println(arr[i]);
		}
		System.out.println("**********");
		
		short[] arr1 = new short[4];
		for(int i = 0;i < arr1.length;i++){
			System.out.println(arr1[i]);
		}
		System.out.println("**********");
		float[] arr2 = new float[5];
		for(int i = 0;i < arr2.length;i++){
			System.out.println(arr2[i]);
		}
		
		System.out.println("**********");
		char[] arr3 = new char[4];
		for(int i = 0;i < arr3.length;i++){
			System.out.println("----" + arr3[i] + "****");
		}
		
		if(arr3[0] == 0){
			System.out.println("Hello!");
		}
		
		System.out.println("**********");
		boolean[] arr4 = new boolean[5];
		System.out.println(arr4[0]);
		
		System.out.println("**********");
		String[] arr5 = new String[5];
		System.out.println(arr5[0]);
		if(arr5[0] == null){
			System.out.println("Nice weather in Beijing!");
		}
	}

2, 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

3. Default initialization value of array element

  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.

4. Memory parsing of array

4, Two dimensional array code example

public static void main(String[] args) {
		//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();
		}
		
	}
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
		
	}

5, Use of array: Please distinguish the key points (see Figure 1 *, 4 * *)

1. Print a 10 Line Yang Hui triangle using a two-dimensional array.

[prompt]

  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, for elements other than the first and last elements. Namely:
    yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
    Code example:
public class YangHuiTest {
	
	public static void main(String[] args) {
		//1. Declare and initialize a two-dimensional array
		int[][] yangHui = new int[10][];
		
		//2. Assign values to the elements of the array
		for(int i = 0;i < yangHui.length;i++){
			yangHui[i] = new int[i + 1];
			
			//2.1 assigning values to the first and last elements
			yangHui[i][0] = yangHui[i][i] = 1;
			//2.2 assign values to non first and last elements of each line
			//if(i > 1){
			for(int j = 1;j < yangHui[i].length - 1;j++){
				yangHui[i][j] = yangHui[i-1][j-1] + yangHui[i-1][j];
			}
			//}
		}
		//3. Traverse two-dimensional array
		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 results:

1     
1     1     
1     2     1     
1     3     3     1     
1     4     6     4     1     
1     5     10     10     5     1     
1     6     15     20     15     6     1     
1     7     21     35     35     21     7     1     
1     8     28     56     70     56     28     8     1     
1     9     36     84     126     126     84     36     9     1     

2. Examination of the algorithm: find the maximum, minimum, average, sum, etc. of the elements in the numerical array
Define a one-dimensional array of int type, including 10 elements, assign some random integers respectively, and then calculate the maximum value, minimum value, sum value and average value of all elements, and output them. Requirement: all random numbers are two digits.

package com.atguigu.java;
/*
 * Examination of the algorithm: find the maximum, minimum, average, sum, etc. of the elements in the numerical array
 * 
 * Define a one-dimensional array of int type, containing 10 elements, and assign some random integers respectively,
 * Then find the maximum, minimum, sum and average values of all elements and output them.	
 * Requirement: all random numbers are two digits.
 * 
 * [10,99]
 * Formula: (int)(Math.random() * (99 - 10 + 1) + 10)
 * 
 */
public class ArrayTest1 {
	public static void main(String[] args) {
		int[] arr = new int[10];
		
		for(int i = 0;i < arr.length;i++){
			arr[i] = (int)(Math.random() * (99 - 10 + 1) + 10);
		}
		
		//ergodic
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
		System.out.println();
		
		//Find the maximum value of array elements
		int maxValue = arr[0];
		for(int i = 1;i < arr.length;i++){
			if(maxValue < arr[i]){
				maxValue = arr[i];
			}
		}
		System.out.println("The maximum value is:" + maxValue);
		
		//Find the minimum value of array elements
		int minValue = arr[0];
		for(int i = 1;i < arr.length;i++){
			if(minValue > arr[i]){
				minValue = arr[i];
			}
		}
		System.out.println("The minimum value is:" + minValue);
		//Sum array elements
		int sum = 0;
		for(int i = 0;i < arr.length;i++){
			sum += arr[i];
		}
		System.out.println("The sum is:" + sum);
		//Find the average number of array elements
		int avgValue = sum / arr.length;
		System.out.println("The average is:" + avgValue);
	}
}

3. The difference between array assignment and array assignment
Assignment:

package com.atguigu.exer;
/*
 * Use simple arrays
(1)Create a class named ArrayExer2 and declare two variables array1 and array2 in the main() method. They are arrays of type int [].
(2)Using curly braces {}, initialize array1 to 8 prime numbers: 2,3,5,7,11,13,17,19.
(3)Displays the contents of array1.
(4)Assign the array2 variable equal to array1, and modify the even index element in array2 to make it equal to the index value (for example, array[0]=0,array[2]=2). Print out array1.
 * 
 * Thinking: what is the relationship between array1 and array2? Array1 and array2 have the same address values and both point to a unique array entity in heap space.
 * Expansion: modify the title to realize the replication of array2 to array1 array
 */
public class ArrayExer2 {
	public static void main(String[] args) {  //alt + /
		int[] array1,array2;
		
		array1 = new int[]{2,3,5,7,11,13,17,19};
		
		//Display the contents of array1
		for(int i = 0;i < array1.length;i++){
			System.out.print(array1[i] + "\t");
		}
		
		//Assign the array2 variable equal to array1
		//Cannot be called a copy of an array.
		array2 = array1;
		
		//Modify the even index element in array2 to make it equal to the index value (for example, array[0]=0,array[2]=2)
		for(int i = 0;i < array2.length;i++){
			if(i % 2 == 0){
				array2[i] = i;
			}
			
		}
		System.out.println();
		//Print out array1
		for(int i = 0;i < array1.length;i++){
			System.out.print(array1[i] + "\t");
		}
	}
}

Copy:

package com.atguigu.exer;
/*
 * Use simple arrays
 * Expansion: modify the title to copy array2 to array1 array
 */
public class ArrayExer3 {
	public static void main(String[] args) {  //alt + /
		int[] array1,array2;
		
		array1 = new int[]{2,3,5,7,11,13,17,19};
		
		//Display the contents of array1
		for(int i = 0;i < array1.length;i++){
			System.out.print(array1[i] + "\t");
		}
		
		//Copy of array:
		array2 = new int[array1.length];
		for(int i = 0;i < array2.length;i++){
			array2[i] = array1[i];
		}
		//Modify the even index element in array2 to make it equal to the index value (for example, array[0]=0,array[2]=2)
		for(int i = 0;i < array2.length;i++){
			if(i % 2 == 0){
				array2[i] = i;
			}
		}
		System.out.println();
		//Print out array1
		for(int i = 0;i < array1.length;i++){
			System.out.print(array1[i] + "\t");
		}
	}
}

(commonly used) 4. Algorithm examination: array copy, inversion and search (linear search and dichotomy search)

package com.atguigu.java;
/*
 * Algorithm examination: array copy, inversion and search (linear search and dichotomy search)
 * 
 * 
 */
public class ArrayTest2 {
	
	public static void main(String[] args) {
		
		String[] arr = new String[]{"JJ","DD","MM","BB","GG","AA"};
		
		
		//Copy of array (different from the assignment of array variable: arr1 = arr)
		String[] arr1 = new String[arr.length];
		for(int i = 0;i < arr1.length;i++){
			arr1[i] = arr[i];
		}
		
		//Inversion of array
		//Method 1:
//		for(int i = 0;i < arr.length / 2;i++){
//			String temp = arr[i];
//			arr[i] = arr[arr.length - i -1];
//			arr[arr.length - i -1] = temp;
//		}
		
		//Method 2:
//		for(int i = 0,j = arr.length - 1;i < j;i++,j--){
//			String temp = arr[i];
//			arr[i] = arr[j];
//			arr[j] = temp;
//		}
		
		//ergodic
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
		
		System.out.println();
		//Find (or search)
		//Linear lookup:
		String dest = "BB";
		dest = "CC";
		
		boolean isFlag = true;
		
		for(int i = 0;i < arr.length;i++){
			
			if(dest.equals(arr[i])){
				System.out.println("The specified element was found at:" + i);
				isFlag = false;
				break;
			}
			
		}
		if(isFlag){
			System.out.println("I'm sorry I didn't find it!");
			
		}
		//Dichotomy search: (familiar)
		//Premise: the array to be found must be ordered.
		int[] arr2 = new int[]{-98,-34,2,34,54,66,79,105,210,333};
		
		int dest1 = -34;
		dest1 = 35;
		int head = 0;//Initial first index
		int end = arr2.length - 1;//Initial last index
		boolean isFlag1 = true;
		while(head <= end){
			
			int middle = (head + end)/2;
			
			if(dest1 == arr2[middle]){
				System.out.println("The specified element was found at:" + middle);
				isFlag1 = false;
				break;
			}else if(arr2[middle] > dest1){
				end = middle - 1;
			}else{//arr2[middle] < dest1
				head = middle + 1;
			}

			
		}
		
		if(isFlag1){
			System.out.println("I'm sorry I didn't find it!");
		}
		
		
	}
}

See the next article for the top ten sorting methods of arrays

Keywords: Java Algorithm

Added by hbalagh on Wed, 26 Jan 2022 14:28:26 +0200