Third set of arrays

Basic concept and function of array

java array stores a group of data with the same data type. The space in memory is continuous. Once the length is given, it cannot be changed

Arrays are reference data types (objects), but they can store basic types or reference types

Array creation

Declaration array

Int means that the data type that can be stored in the array is int

int[] a;

Create array

Default value

Integer: 0

Floating point: 0.0

char: Space

Boolean: false

Reference type: null

       //Create array mode 1: dynamic creation
        int[] a = new int[10];//Create a continuous space with a length of 10
        System.out.println(Arrays.toString(a));//Output array objects as strings
​
       //Mode 2
        char x = 'a';//97
        int [] b = new int[]{1,2,3,4,x};
        System.out.println(Arrays.toString(b));
​
       //Mode 3
        int [] c = {1,2,3,4,5};
        System.out.println(Arrays.toString(c));
​
        String[] s = new  String[]{"a","b","c"};
        System.out.println(Arrays.toString(s));

Length of array:

length attribute

int [] b1 = new int []{1,2,3,4,5,6,7};

System.out.println(b1.length);

Access and iteration of array

● access to array elements:

• array name [index]

For example: a[0],a[1];

• note:

• the index of the array starts from 0.

• the data type of the index is integer

• the difference between the maximum index value and the array length is always 1

int [] a = new int[5];
               a[0] = 1;
               a[1] = 2;
        System.out.println(a[0]);
        System.out.println(a[1]);
               a[4] = 5;

When accessing array elements, be aware that the array is out of bounds

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
           
              at day1.Demo2.main(Demo2.java:18)

Traversal of array (iteration)

Output array

      int [] a = new int[5];
         for(int i=0;i<=4;i++){
             a[i] = i+2;
         }
         System.out.println(Arrays.toString(a));

for loop

 int []a = {2,3,4,1,5};
 for (int i = 0; i < a.length; i++) {
             System.out.println(a[i]);

Loop enhancement for

int []a = {2,3,4,1,5};
for(int t : a){
             System.out.println(t);
         }

Array sorting

Bubble sorting

Take out two adjacent elements for comparison each time

Move the larger back until all the elements are compared

       int [] a = {4,3,5,2,1};
                 // 3 4 2 1 5
                 // 3 2 1 4 5
                 // 2 1 3 4 5
                 // 1 2 3 4 5
        for (int i = 0; i < a.length-1; i++) {//Outer cycle, control cycle several times
            for (int j=0;j<a.length-1-i;j++){//The inner layer controls the times of each comparison in sequence
               if(a[j]>a[j+1]){
                   int temp= a[j];
                   a[j] = a[j+1];
                   a[j+1] = temp;
               }
            }
        }
​
        System.out.println(Arrays.toString(a));

Select sort

Start from the first one and compare with each subsequent element. If the conditions are met, the larger / smaller ones will be swapped

The first way

Exchange directly after each comparison, and exchange several times

    int []a = {3,1,2,6,4,5};
        for (int i = 0; i < a.length-1; i++) {//Take out the position to be compared in the array
            for (int j = i+1; j <a.length; j++) {//Each number after the extraction position is compared with the extracted value in turn
                if(a[i]>a[j]){
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }

The second way

Use minIndex to record the index of the smallest number to reduce the number of exchanges

    int []a = {3,1,2,6,4,5};
        for (int i = 0; i < a.length-1; i++) {//Take out the position to be compared in the array
            int minIndex = i;//Minimum index value of index record
            for (int j = i+1; j <a.length; j++) {//Each number after the extraction position is compared with the extracted value in turn
                if(a[j]<a[minIndex]){//Assign a smaller number of indexes to minIndex
                    minIndex=j;
                }
            }
            //After the internal loop comparison, exchange the elements with indexes of minIndex and I (i.e. exchange the minimum value to the position of I)
            int temp = a[minIndex];
            a[minIndex] = a[i];
            a[i] = temp;
        }

Although there are more code statements in the second method, the number of exchanges is less and the running time is relatively shorter

Insert sort

In a group of numbers that need to be sorted, assuming that the first n-1 numbers have been sorted, now insert the nth number into the previous ordered sequence so that these n numbers are sorted.

Cycle until all are in order

The first way

Compare and exchange the nth number to be inserted with the previous one

    int []a = {3,2,1,5,4};
​        for (int i = 1; i<a.length; i++) {
            for (int j = i; j >0 ; j--) {
                if(a[j]<a[j-1]){
                    int t = a[j];
                    a[j] = a[j-1];
                    a[j-1] = t;
                }
            }
        }

The second way

Two variables are used. The first records the number of inserts to be inserted and the second records the current position compared

If the value of the comparison position and the number of inserts are compared, the result is the desired result and the comparison position is in the array

Give the value of the comparison position to perIndex+1 (that is, move the value of perIndex back to leave this position empty)

After the cycle conditions are not met, put the number to be inserted in the last one of the comparison positions (the comparison position will jump out of the cycle only after taking one more step)

  int []a = {3,2,1,5,4};
      for (int i = 1; i <a.length; i++) {
            int correntvalue = a[i];//Number of records to be inserted for sorting
            int perIndex = i-1;//Record the current compared position
            while (perIndex>=0 && correntvalue<a[perIndex]){
                a[perIndex+1] = a[perIndex];
                perIndex--;
            }
            a[perIndex+1] = correntvalue;
        }

Two dimensional array

Two dimensional array definition

A two-dimensional array is an array of arrays

Each element of a two-dimensional array is a one-dimensional array

Declaration of two-dimensional array

int [][] a;
int a2[][];
//Generally, the first one is selected, which is not easy to confuse the data type of a

Creation of two-dimensional array

int [][]a = new int[][]{{1,2,3},{1,2,3},{1,2,3}};
int [] [] b = {{1,2,3},{1,2,3},{1,2,3}}; 
int [][] c = new int[3][5];//There are three one-dimensional arrays with length of 5 in the two-dimensional array




int [][]d = new int[3][];
        /*
        It only declares that the length of the two-dimensional array is 3, but the value of the one-dimensional array is null
        Output d, the result is [null,null,null];
        When assigning values to the following one-dimensional array, you can specify the length of the one-dimensional array yourself*/

Iteration of two-dimensional array

int [][]a = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.println(a[i][j]);
            }
        }

Debug debugging in development tools

Breakpoints (red circle in idea) set breakpoints when you need to debug code

Using Debug mode

F8 step over is debugged step by step and will not enter the called method

shift+F8 step out jump out debugging

F7 step into the called method

Keywords: Java Back-end

Added by Tjk on Wed, 16 Feb 2022 09:12:55 +0200