040_ array

Time:2020-12-1

array

https://www.bilibili.com/video/BV12J41137hu/?p=51

Definition of array

An array is an ordered collection of data of the same type.

Array creation

//Static initialization
int[] nums1 = {1,2,3};
//Dynamic initialization
int[] nums2 = new int[10];
nums2[0] = 1;
nums2[1] = 2;

Characteristics of arrays

  1. Its length is determined. Once an array is created, its size cannot be changed.
  2. Its elements must be of the same type. Mixed types are not allowed.
  3. The elements in an array can be any data type, including the base type and the reference type.
  4. The array variable belongs to the reference type, and the array can be regarded as an object. Each element in the array is equivalent to the member variable of the object. The array itself is an object. In Java, the object is in the heap. Therefore, whether the array stores the basic type or the reference type, the array object itself is in the heap.

Use of arrays

  1. For loop or for each loop
  2. Array as method input parameter
  3. Array as the return value

Two dimensional array

int[][] nums = {{1,2},{2,3},{3,4}};
int[][] nums = new int[3][2];
nums[0] = {1,2};
nums[0][0] = 1;

Arrays class

  1. The utility class of the array java.util.Arrays 。
  2. Check the JDK help documentation or view the source code.

Bubble sort

package com.qing.array;

import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) {
        int[] array = {12,455,22,23,25,3,443,44};
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(sort(array)));
    }
    /*
    Bubble sort
    1. From the beginning, make a big exchange
    2. Outer loop: the number of cycles, length - 1, and the last digit does not need to be compared
    3. Inner loop: the number of times of comparison, the length of the first time is - 1, decreasing in turn. A comparison will produce a maximum value, and there is no need to compare again
     */
    public static int[] sort(int[] array) {
        int temp = 0;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
            }
        }
        return array;
    }
}
[12, 455, 22, 23, 25, 3, 443, 44]
[3, 12, 22, 23, 25, 44, 443, 455]

Simple optimization

package com.qing.array;

import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) {
//        int[] array = {12,455,22,23,25,3,443,44};
        int[] array = {12,45,12,11,125,133,143,144};
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(sort(array)));
        int[] array1 = {12,45,12,11,125,133,143,144};
        System.out.println(Arrays.toString(sort1(array1)));
    }
    /*
    Bubble sort
    1. From the beginning, make a big exchange
    2. Outer loop: the number of cycles, length - 1, and the last digit does not need to be compared
    3. Inner loop: the number of times of comparison, the length of the first time is - 1, decreasing in turn. A comparison will produce a maximum value, and there is no need to compare again
    Simple optimization
    4. If one round of comparison is not exchanged, it indicates that the sorting has been successful and there is no need to compare again
     */
    public static int[] sort1(int[] array) {
        int temp = 0;
        int count = 0;
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                    flag = false;
                }
            }
            count+=1;
            if (flag) {
                System.out.println ("sort1 cycle number: + (count));
                return array;
            }
        }
        System.out.println ("sort1 cycle number: + (count));
        return array;
    }
    /*
    Bubble sort
    1. From the beginning, make a big exchange
    2. Outer loop: the number of cycles, length - 1, and the last digit does not need to be compared
    3. Inner loop: the number of times of comparison, the length of the first time is - 1, decreasing in turn. A comparison will produce a maximum value, and there is no need to compare again
     */
    public static int[] sort(int[] array) {
        int temp = 0;
        int count = 0;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
            }
            count+=1;
        }
        System.out.println ("number of sort cycles?" + (count));
        return array;
    }

}
[12, 45, 12, 11, 125, 133, 143, 144]
Number of sorting cycles: 7
[11, 12, 12, 45, 125, 133, 143, 144]
Sort1 cycle number: 4
[11, 12, 12, 45, 125, 133, 143, 144]

Sparse array

  1. When most elements in an array are 0 or the same value, sparse array can be used to save the array.
  2. Processing method of sparse array:
    1. The first row of record array has several rows and columns, and how many different values there are.
    2. Record elements and columns and values with different values in a small array to reduce the size of the program.
  3. As shown in the following figure: the left side is the original array, and the right side is the sparse array.

image.png

package com.qing.array;

public class Demo02 {
    public static void main(String[] args) {
        //1. Create a two-dimensional array, record the chess game, 11 * 11, 0: no pieces, 1: black, 2: white
        int[][] arr1 = new int[11][11];
        arr1[1][2] = 1;
        arr1[2][3] = 2;
        System.out.println ("output original array");
        for (int[] ints : arr1) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
        System.out.println("=========================");
        /*
        2. Convert to sparse array
        The sparse array has 3 columns and 1 rows.
        The first row of record array has several rows and columns, and how many different values there are.
         */
        int sum = 0;
        //Calculate the valid value of the original array
        for (int[] ints : arr1) {
            for (int anInt : ints) {
                if (anInt != 0) {
                    sum ++;
                }
            }
        }
        System.out.println ("original array valid value: + sum)";
        //The sparse array has 3 columns and 1 rows.
        //The first row of record array has several rows and columns, and how many different values there are.
        int[][] arr2 = new int[sum + 1][3];
        arr2[0][0] = 11;
        arr2[0][1] = 11;
        arr2[0][2] = sum;
        int count = 1;
        //Record the row, column, value of valid value
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                if (arr1[i][j] != 0) {
                    arr2[count][0] = i;
                    arr2[count][1] = j;
                    arr2[count][2] = arr1[i][j];
                    count++;
                }
            }
        }
        System.out.println ("output sparse array");
        for (int[] ints : arr2) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
        System.out.println("===================");
        //3. Restore the original array
        int[][] arr3 = new int[arr2[0][0]][arr2[0][1]];
        for (int i = 1; i < arr2.length; i++) {
            arr3[arr2[i][0]][arr2[i][1]] = arr2[i][2];
        }
        System.out.println ("output restored original array");
        for (int[] ints : arr3) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
    }
}
Output the original array
0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	2	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
=========================
Valid value of original array: 2
Output sparse array
11	11	2	
1	2	1	
2	3	2	
===================
Output restored original array
0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	2	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0