1, Concept
-
An array is a collection of multiple data types arranged in a certain order, named with a name, and managed uniformly by numbering.
-
The array itself is a reference data type, and the elements in the array can be any data type, including basic data type and reference data type.
-
Creating an array object will open up a continuous memory space in the content, and the first address of this continuous space is referenced in the array name.
-
Once the length of the array is determined, it cannot be modified.
-
data classification
- Divided by dimension: one-dimensional array, two-dimensional array
- By element type: array of basic data type elements and array of reference data type elements
2, One dimensional array
1. Declaration and initialization
public class ArrayTest { public static void main(String[] args) { // 1. Static initialization: array initialization and array element assignment are performed simultaneously int[] ids; // statement ids = new int[]{1001, 1002, 1003, 1004}; // 2. Dynamic initialization: array initialization and array element assignment are performed separately String[] names; names = new String[5]; // Declaration + initialization String[] animals = new String[10]; char[] grades = new char[]{'A', 'B', 'C', 'D'}; // Other writing methods int[] nums = {1, 2, 3, 4}; // Type inference } }
2. Array index
- The element at the specified position of the data is called by index.
- The index of the array starts at 0 and ends at array length - 1.
package com.example.www; public class ArrayTest { public static void main(String[] args) { String[] names; names = new String[5]; names[0] = "Guan Yu"; names[1] = "Fei Zhang"; names[2] = "Huang Zhong"; names[3] = "ma chao"; names[4] = "Zhao Yun"; } }
3. Array length
public class ArrayTest { public static void main(String[] args) { String[] names; names = new String[5]; System.out.println(names.length); // 5 } }
4. Array traversal
public class ArrayTest { public static void main(String[] args) { String[] names; names = new String[5]; names[0] = "Guan Yu"; names[1] = "Fei Zhang"; names[2] = "Huang Zhong"; names[3] = "ma chao"; names[4] = "Zhao Yun"; for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } } }
5. Default initialization value
public class ArrayTest { public static void main(String[] args) { String[] names; names = new String[5]; for (int i = 0; i < names.length; i++) { System.out.println(names[i]); // null } } }
Array element is an integer: 0
Array elements are floating point: 0.0
Array elements are of type char: 0 (null character) in ASCII
Array elements are boolean: false
Array element is a reference data type: null
6. Array memory parsing
3, Two dimensional array
1. Declaration and initialization
public class ArrayTest { public static void main(String[] args) { // 1. Static initialization int[][] arr1 = new int[][]{{1,2,3},{4,5},{6}}; // 2. Dynamic initialization int[][] arr2 = new int[3][]; int[][] arr3 = new int[3][2]; // Other writing methods int arr4[][] = new int[3][]; int[] arr5[] = new int[3][]; int[][] arr6 = {{1,2,3},{4,5},{6}}; } }
2. Array index
public class ArrayTest { public static void main(String[] args) { int[][] arr1 = new int[][]{{1,2,3},{4,5},{6}}; System.out.println(arr1[0][1]); // 2 int[][] arr2 = new int[3][]; arr2[1] = new int[4]; System.out.println(arr2[1][0]); // 0 } }
3. Array length
public class ArrayTest { public static void main(String[] args) { int[][] arr1 = new int[][]{{1,2,3},{4,5},{6}}; System.out.println(arr1.length); // 3 System.out.println(arr1[1].length); // 2 } }
4. Array traversal
public class ArrayTest { public static void main(String[] args) { int[][] arr = new int[][]{{1,2,3},{4,5},{6}}; 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(); } } } //1 2 3 //4 5 //6
5. Default initialization value
public class ArrayTest { public static void main(String[] args){ // Initialization method 1: int[][] arr1 = new int[4][3]; System.out.println(arr1[0]); // [ I@1b6d3586 Address value System.out.println(arr1[0][0]); // 0 System.out.println(arr1); // [[I@4554617c // Initialization mode 2: int[][] arr2 = new int[4][]; System.out.println(arr2[0]); // null System.out.println(arr2[0][1]); // NullPointerException, cannot be called, and an error is reported } }
6. Array memory parsing
4, Arrays tool class
import java.util.Arrays; public class ArrayTest { public static void main(String[] args) { int[] arr1 = new int[]{1, 2, 3, 4}; int[] arr2 = new int[]{2, 1, 4, 3}; boolean isEquals = Arrays.equals(arr1,arr2); // Determine whether the arrays are equal System.out.println(isEquals); // false // Output array information System.out.println(Arrays.toString(arr1)); // [1, 2, 3, 4] // Fills the array with the specified value Arrays.fill(arr1, 10); System.out.println(Arrays.toString(arr1)); // [10, 10, 10, 10] // sort Arrays.sort(arr2); System.out.println(Arrays.toString(arr2)); // [1, 2, 3, 4] // Binary search int[] arr3 = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 21, 23}; int index = Arrays.binarySearch(arr3, 17); System.out.println(index); // 6 } }
5, Array exercise
1. Yanghui triangle
// Investigate element assignment 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 assign values to the first and last elements first 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(); } } }
2. Find the maximum, minimum, average and sum of elements in a numeric array
Define a one-dimensional array of int type, which contains 10 elements and is given two random integers respectively. Then calculate the maximum, minimum, average and sum of all elements and output them.
//Obtain the random number of [a,b] range: (int)(Math.random()*(b-a+1)+a) public class ArrayTest { 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); } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); //Maximum 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); //minimum value 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 int sum = 0; for (int i = 0; i < arr.length; i++) { sum += arr[i]; } System.out.println("And:" + sum); //average int avgValue = sum / arr.length; System.out.println("The average is:" + avgValue); } }
3. Array copy
public class ArrayTest { public static void main(String[] args) { int[] arr1,arr2; arr1 = new int[]{2, 3, 5, 7, 11, 13, 17, 19}; // Display the contents of arr1 for (int i = 0; i < arr1.length; i++) { System.out.print(arr1[i] + " "); // 2 3 5 7 11 13 17 19 } System.out.println(); // 1. Assign arr2 variable equal to arr1 // It cannot be called array copy. The address values of arr1 and arr2 are the same // arr2 = arr1; // 2. Traversal assignment arr2 = new int[arr1.length]; for (int i = 0; i < arr1.length; i++) { arr2[i] = arr1[i]; } // Modify elements in arr2 for (int i = 0; i < arr2.length; i++) { if (i % 2 == 0){ arr2[i] = i; } } // Display the contents of arr1 for (int i = 0; i < arr1.length; i++) { System.out.print(arr1[i] + " "); // 2 3 5 7 11 13 17 19 } System.out.println(); // Displays the contents of arr2 for (int i = 0; i < arr2.length; i++) { System.out.print(arr2[i] + " "); // 0 3 2 7 4 13 6 19 } System.out.println(); } }
4. Array inversion
public class ArrayTest { public static void main(String[] args) { int[] arr = new int[]{2, 3, 5, 7, 11, 13, 17, 19}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); // 2 3 5 7 11 13 17 19 } System.out.println(); // Array inversion // Method 1 // for (int i = 0; i < arr.length / 2; i++) { // int tmp = arr[i]; // arr[i] = arr[arr.length - i - 1]; // arr[arr.length - i - 1] = tmp; // } // Method 2 for (int i = 0, j = arr.length - 1; i<j;i++,j--) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); // 19 17 13 11 7 5 3 2 } } }
5. Find
5.1 linear search
public class SearchTest { public static void main(String[] args) { int[] arr = new int[]{2, 3, 5, 7, 11, 13, 17, 19}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); // 2 3 5 7 11 13 17 19 } System.out.println(); int dest = 17; // Linear search for (int i = 0; i < arr.length; i++) { if (dest == arr[i]){ System.out.println("Index:"+i); break; } } } }
5.2 dichotomy
Premise: the array to be found must be ordered
public class SearchTest { public static void main(String[] args) { int[] arr = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 21, 23}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); // 2 3 5 7 11 13 17 19 } System.out.println(); int dest = 17; // binary search int head = 0; // Initial first index int end = arr.length - 1; // Initial last index boolean isFlag = true; while (head <= end){ int middle = (head + end) / 2; if (dest == arr[middle]){ System.out.println("Index:" + middle); isFlag = false; break; }else if (arr[middle] > dest) { end = middle -1 ; }else { head = middle + 1; } } if(isFlag){ System.out.println("Not found"); } } }
6. Sorting
6.1 bubble sorting
public class BubbleSortTest { public static void main(String[] args) { int[] arr = new int[]{11, 17, 2, 13, 5, 23, 7, 3}; // Bubble sorting for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j+1]) { int tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); // 2 3 5 7 11 13 17 23 } } }