JAVA learning notes 2 array

Array

  • An array is an ordered collection of data of the same type
  • The array length is fixed
  • Array can be any data type, including basic type and reference type
  • The array object itself is in the heap
  • The subscript of the array starts from 0
  • Array subscripts must be used within the specified range, otherwise an error is reported (subscript out of bounds exception)
    • That is, int[]arr=new int[5]; Its valid subscripts are 0-4
    • When an array is a reference type, the array data is an object

Array declaration and creation

definition:

int[]nums;//Preferred claim type
int nums2[];//C language style 
nums=new int[10];//Create an array
int [] num=new int [10];//Combination of creation and declaration
int [] numbers={10,20,30,40,50};//Define array:

Array assignment mechanism (reference passing)

  • Basic data type assignment (value copy)
int n1=10;
int n2=n1;
n2=80;
//Output n1 is 10; n2 is 80;
//The change of n2 will not affect the value of n1
  • Array reference passing (address copy)
int[] arr1={1,2,3};
int[] arr2=arr1;
//Given an address, the change of arr2 will affect arr1
arr2[0]=10;
//The output arr1 is 10 2 3

Array length

Get the length of the array: arrays length

initialization

//Static initialization:
int[]a={1,2,3,4,5,6};
System.out.println(a[0]);
//Dynamic initialization: including default initialization (once the space is allocated, those that have not been assigned are 0 or NULL)
int[]b=new int[10];
b[0]=10;

Array boundary

Legal range of subscript: [0,length-1]
If it is out of bounds, an error will be reported: ArrayIndexOutOfBoundException: array subscript out of bounds exception

Array usage

    int[]arrays={1,2,3,4,5};
    //Print all array elements
    for(int i=0;i<arrays.length;i++){
        System.out.println(arrays[i]);
    }
    //Calculate the sum of all elements
    int sum=0;
    for(int i=0;i<arrays.length;i++){
        sum+=array[i];
    }
    //Find the largest element
    int max=array[0];
    for(int i=1;i<arrays.length;i++){
    if(arrays[i]>max){
        max=arrays[i];}
    }
    //Invert array
    int[]result=new int[arrays.length];
    for (int i = 0,j=result.length-1; i <arrays.length ; i++,j--) {
         result[j]=arrays[i];   
        }
        return result;

Enter arrays For automatically generates an enhanced for loop

    for(int array:arrays){
    System.out.println(arrays);
    //No subscript
    }

Multidimensional array

  • Multidimensional arrays can be regarded as arrays of arrays
  • Take a two-dimensional array as an example
    • Formally, int [] []
    • arr[i] represents the i+1 element of the two-dimensional array. eg: arr[0] is the first element of the two-dimensional array
    • arr[i].length gets the length of each corresponding one-dimensional array
    • Access the j + 1st value arr[i][j] of the (I + 1st) one-dimensional array
//Definition of two-dimensional array:
int[][]arrays={{1,2}{2,3}{3,4}};
//An array with three rows and two columns. arrays[0][0] is 1 and arrays[1][1] is 3
// 1,2     
// 2,3
// 3,4
//At this time, the length of the printed array is 3, that is, there are three elements in the outer space

Tool class Arrays

Avoid making wheels repeatedly. If you have tools, you don't have to do it again.
Common functions:

  • Assign value to array: fill
  • Output array: toString
  • Sort array: sort
  • Comparison array: equals
  • Find array elements: binarySearch
    int []a={1,3,23,5235,2432,4234};
    //Print array elements: arrays toString
    System.out.println(Arrays.toString(a));
    //The results are [1, 3, 23, 5235, 2432, 4234]
    //Sorting algorithm:   
    Arrays.sort(a);//Ascending order
    //The results printed later are [1, 3, 23, 2432, 4234, 5235]
    //Array fill
    Arrays.fill(a,0);//Fill in all as 0
    //Then the print result is [0, 0, 0, 0, 0, 0]
    Arrays.fill(a,1,3,0);//Enter 0 between the values of subscript 1 and subscript 3
    //Then the print result is [1, 0, 0, 5235, 2432, 4234]

Sparse array

A data structure, a two-dimensional array. Many elements have a default value of 0 or the same value.

Treatment method:

  • Record the rows and columns of the array and how many different values there are
  • The elements, rows, columns and values with different values are recorded in a small array, so as to reduce the size of the program

[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG lrsldwwf-164308226002) (assets / sparse array. jpg)]

//Create a two-dimensional array 11 * 11 0: no pieces, 1: black, 2: white
        int[][]array1=new int[11][11];
        array1[1][2]=1;
        array1[2][3]=2;
        System.out.println("Output original array");
        for (int[] ints : array1) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
            //The output result is:
// 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

        }
        //Convert to sparse array save
        //Get the number of valid values
        int sum=0;
        for(int i=0;i<11;i++){
            for(int j=0;j<11;j++){
                if(array1[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("Number of valid values:"+sum);
        //Create an array of sparse arrays
        int[][]array2=new int [sum+1][3];
        array2[0][0]=11;
        array2[0][1]=11;
        array2[0][2]=sum;
        //Traverse the two-dimensional array array1 and store the non-zero value in the sparse array
        int count=0;
        for(int i=0;i< array1.length;i++){
            for(int j=0;j<array1[i].length;j++){
                if(array1[i][j]!=0){
                    count++;
                    array2[count][0]=i;
                    array2[count][1]=j;
                    array2[count][2]=array1[i][j];
                }
            }
        }
        //Output sparse array
        System.out.println("Sparse array:");
        for(int i=0;i<array2.length;i++){
            System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]+"\t");
        }
        //Sparse array:
        // 11	11	2	
        // 1	2	1	
        // 2	3	2	

        //Give a sparse array to restore the original array:
        //1. Read sparse array
        int[][]array3=new int[array2[0][0]][array2[0][1]];
        //2. Restore its value to its element
        for(int i=1;i<array2.length;i++){
            array3[array2[i][0]][array2[i][1]]=array2[i][2];
        }
        //3. Print
        for (int[] ints : array3 ){
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
            }
//The output result is
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	

    }

Keywords: Java Back-end

Added by trehy2006 on Wed, 26 Jan 2022 06:37:34 +0200