An array of

1, the array

1.

  • An array is a collection of data of the same type arranged in a certain order. The array is ordered
  • An array is a reference data type. The elements in an array can be of any data type, but an array can only store data of the declared type
  • A variable that refers to a type may store only two types of values: null or address values (containing the type of the variable)
  • A contiguous block of memory whose initial address is referenced by the array name
  • Once the length of an array is determined, it cannot be modified
  • You can use an array index (subscript) to call an element at a specified location, which is fast
  • From the bottom of the array operation mechanism, there is no multidimensional array. For a two-dimensional array, you can think of the inner one-dimensional array as an element of the outer one-dimensional array

2. To create

3. Two-dimensional arrays

/* * 2d array * 1. Static initialization * 2. Dynamic initialization * 3. Index: How to call the element at the specified position * 4. How to get the length of the array * 5. How do I iterate over groups */
@Test
public void test1(a){
    //1. Static initialization
    int[][] arr1 = new int[] [] {{1.2.3}, {1.2.3.2.3}, {1}};
    int[] arr2[] = new int[] [] {{1.2.3}, {1.2.3.2.3}, {1}};
    int[][] arr3 = {{1.2.3}, {1.2.3.2.3}, {1}};

    //2. Dynamic initialization
    // The initial length of the inner array is 2, which can be changed by explicit assignment; And because the inner array has been allocated memory space, is the address value
    String[][] arr4 = new String[3] [2];
    String[][] arr5 = new String[3] [];// The address of the inner array is null because there is no memory space
    System.out.println(arr4.length);/ / 3
    System.out.println(arr4[0].length);/ / 2
    System.out.println(arr4[1].length);/ / 2
    System.out.println(arr4[2].length);/ / 2

    / / 3. Index
    //4. Get the array length
    arr4[0] = new String[]{"ab"."cd"."adf"."adf"};
    System.out.println(arr4[0].length);/ / 4
    System.out.println(arr4[1].length);/ / 2
    System.out.println(arr4[2].length);/ / 2
    System.out.println(arr4[0] [3]);//adf

    System.out.println(arr4[0]);/ / address values
    System.out.println(arr4[1]);/ / address values
    System.out.println(arr4[2]);/ / address values

    System.out.println(arr5[0]);/ / null null pointer
    System.out.println(arr5[1]);/ / null null pointer
    System.out.println(arr5[2]);/ / null null pointer
    System.out.println(arr5[2].toString());//java.lang.NullPointerException
    
    // Space must be created before assignment
	//arr5[0][0] = "a";
	//System.out.println(arr5[0][0]); //NullPointerException
    arr5[0] = new String[3];
    arr5[0] [0] = "a";
    System.out.println(arr5[0] [0]);//a
}

@Test
public void test2(a){
    //5. Iterate over the two-dimensional array
    int[][] arr = {{1.2.3}, {1.2.3.2.3}, {1}};
    System.out.print("[");
    for(int i=0; i<arr.length; i++){
        System.out.print("[");
        for(int j=0; j<arr[i].length; j++){
            if(j == arr[i].length-1){
                System.out.print(arr[i][j]);
            }else{
                System.out.print(arr[i][j] + ""); }}if(i == arr.length-1){
            System.out.print("]");
        }else{
            System.out.print("].");
        }
    }
    System.out.print("]");
}//[1 2 3], [1 2 3 2 3], [1]]
Copy the code

4. Default initialization values for array elements

5. Array memory parsing

The Arrays utility class

@Test
public void test1(a){
    int[] arr1 = new int[] {1.2.3.4};
    int[] arr2 = new int[] {1.2.3.4};

    boolean isEquals = Arrays.equals(arr1, arr2);
    System.out.println(isEquals);//true
}

@Test
public void test2(a){
    int[] arr1 = new int[] {1.2.3.4};
    System.out.println(Arrays.toString(arr1));/ / [1, 2, 3, 4]
}

@Test
public void test3(a){
    int[] arr1 = new int[] {1.2.3.4};
    Arrays.fill(arr1, 0);
    System.out.println(Arrays.toString(arr1));/ / [0, 0, 0, 0]
}

@Test
public void test4(a){
    int[] arr1 = new int[] {23, -2.32.4};
    Arrays.sort(arr1);
    System.out.println(Arrays.toString(arr1));
}

@Test
public void test5(a){
    int[] arr1 = new int[] {23, -2.32.4};
    int index = Arrays.binarySearch(arr1, 32);

    if(index >= 0){
        System.out.println(index);
    }else{
        System.out.println("Not found."); }}Copy the code

3. Common algorithms

/* * Copy, reverse, sort, search (linear search, binary search) */
//1. Find the maximum value of a one-dimensional array
// Compare the first number arr[0] with the rest of the one-dimensional array. Assign the larger number to x
public int getMax(int[] arr){
    //int maxValue = 0;
    int maxValue = arr[0];
    for(int i=0; i<arr.length; i++){
        if(maxValue < arr[i]){ maxValue = arr[i]; }}return maxValue;
}

//2. Find the minimum of the one-dimensional array
public int getMin(int[] arr){
	//int minValue = 0;
    int minValue = arr[0];
    for(int i=0; i<arr.length; i++){
        if(minValue > arr[i]){ minValue = arr[i]; }}return minValue;
}

//3. Find the sum of one-dimensional array
public int getSum(int[] arr){
    int sum = 0;
    for(int i=0; i<arr.length; i++){
        sum += arr[i];
    }
    return sum;
}

//4. Find the average of the one-dimensional array
public int getAvg(int[] arr){
    return getSum(arr) / arr.length;
}


//1. Copy the array
public String[] copy(String[] arr){
    String[] arr1 = new String[arr.length];
    for(int i=0; i<arr.length; i++){
        arr1[i] = arr[i];
    }
    return arr1;
}


//2. Array inversion
public String[] reverse1(String[] arr){
    for(int i=0; i<arr.length/2; i++){
        String temp = arr[i];
        arr[i] = arr[arr.length-1-i];
        arr[arr.length-1-i] = temp;
    }
    return arr;
}

public String[] reverse2(String[] arr){
    for(int i=0, j=arr.length-1; i<arr.length/2; i++, j--){
        String temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    return arr;
}


// Linear search
public int search(String[] arr, String dest){
    int index = -1;
    for(int i=0; i<arr.length; i++){
        if(dest.equals(arr[i])){
            index = i;
            break; }}return index;
}

Binary search: the array to be searched must be ordered
public int binarySearch(int[] arr, int dest){
    int index = -1;
    int head = 0;
    int end = arr.length-1;
    while (head<=end){
        int middle = (head + end)/2;
        if(dest == arr[middle]){
            index = middle;
            break;
        }else if(dest > arr[middle]){
            head = middle + 1;
        }else {
            end = middle -1; }}return index;
}
Copy the code

Yang hui triangle

/* * Print a 10-line triangle using a two-dimensional array * 1. The first line has 1 element, and the NTH line has n elements * 2. The first and last elements in each line are 1 * 3. Starting on the third line, for elements that are not the first and last elements. Yanghui: * [I] [j] = yanghui [1] [I - 1] + yanghui [I - 1] [j]; * * [0] 1 * [1] 1 1 * [2] 1 21 * [3] 1 3 3 1 * [4] 1 4 6 4 1 * [5] 15 10 10 5 1 * [6] 1 6 15 20 15 6 1 * [7] 1 7 21 35 35 21 7 1 * [8] 1 8 28 56 70 56 28 8 1 * [9] 1 9 36 84 126 126 84 36 9 1 * [0] [1] [2] [3] [5] [6] [7] [8] [9] */

@Test
public void test(a){
    //1. Initialize a two-dimensional array
    int[][] arr = new int[10] [];for(int i = 0; i<arr.length; i++){
        //2. Make space for each inner array
        arr[i] = new int[i+1];
        
        //3. Assign values to the beginning and end of each line
        arr[i][0] = 1;
        arr[i][i] = 1;
        
        //4. Assign the middle element of each line
        for(int j=1; j<arr[i].length-1; j++){
            arr[i][j] = arr[i-1][j-1] + arr[i-1][j]; }}for(int i=0; i<arr.length; i++){
        for(int j=0; j<arr[i].length; j++){
            System.out.print(arr[i][j] + "\t"); } System.out.println(); }}Copy the code

Bubble sort

Iterate through the sequence to be sorted, comparing adjacent elements. If the first is greater than the second (in ascending order), swap them both.

public int[] bubbleSort(int[] arr){
    for(int i=0; i<arr.length; i++){
        // With each trip, the largest is moved to the last
        // Compare adjacent elements
        for(int j=0; j<arr.length-1-i; j++){
            if(arr[j] > arr[j+1]) {int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp; }}}return arr;
}
Copy the code

Recursive method

/* * 1. A method calls itself internally * 2. Method recursion consists of an implicit loop that executes a piece of code over and over again, but this repetition does not require loop control * Recursion must recurse in a known direction, otherwise it becomes infinite recursion, similar to an infinite loop. * /
Example 1: Calculate the sum of all natural numbers between 1 and 100
public int getSum(int n){
    if(n == 1) {return 1;
    }else {
        return n + getSum(n - 1); }}// Example 2: Compute the product of all natural numbers between 1 and n :n!
public int getSum1(int n){
    if(n == 1) {return 1;
    }else {
        return n*getSum1(n-1); }}/ / example 3: there is a known sequence: f (0) = 1, f (1) = 4, f (n + 2) = 2 * f (n + 1) + f (n),
// where n is an integer greater than 0, find f(10).
public int getSum2(int n){
    if(n == 0) {return 1;
    }else if(n == 1) {return 4;
    }
    else{
        return 2*getSum2(n-1) + getSum2(n-2); }}Copy the code