JAVA array declaration, create, use, multidimensional array, Arrays class, bubble sort, sparse array

array

An ordered collection of data of the same type
It describes several data of the same type, which are arranged and combined in a certain order
Among them, each data is called an array element, and each array element can access them through a subscript

  • Array features:
  1. The length is fixed. Once the array is created, its size cannot be changed
  2. Its elements must be of the same type, and mixed types are not allowed
  3. The elements in the array can be any data type (base type and reference type)
  4. Array variables are of reference type. Arrays can also be regarded as objects. Each element in the array is equivalent to the member variables of the object.
    The array itself is an object. In Java, the object is in the heap. Therefore, whether the array saves the original type or other data types, the array object itself is in the heap

Declaration creation

  • Syntax for declaring array variables:

dataType[] arrayRefVar;

  • The Java language uses the new operation symbol to create the syntax of the array:

dataType[] arrayRefVar = new dataType[arraySize] ;

  • Array elements are accessed by index, and the array index starts from 0
  • Get array length: arrays length
  public static void main(String[] args) {
        int[] nums;
        nums = new int[6];
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        nums[5] = 6;

        //Calculate the sum of all elements
        int sum = 0;

        //Get array length: arrays length
        for (int i = 0; i < nums.length; i++) {
            sum = sum + nums[i];
        }
        System.out.println("The sum is"+sum);

    }

// result
 The total is 21
    public static void main(String[] args) {
        //initiate static
        int[] a = {1,2,3,4,5,6};
        System.out.println(a[0]);

        //dynamic initialization
        int[] b = new int[17];
        b[0] = 16;
        System.out.println(b[0]);
    }
// result
1
16
  • Legal range of subscript: [0,length-1]
  • An array is an ordered collection of the same data type (the data type can be any type)
  • Arrays are also objects, and array elements are equivalent to member variables of objects
  • The length of the array is fixed and immutable. If it is out of bounds, it will report ArrayIndexOutOfBoundsException

use

Normal For loop
For each loop
Array as method arguments
Array as return value

    public static void main(String[] args) {
       int[] arrays = {16,17,21,22};
//        // JDK1.5. No subscript, enhanced for loop
//        for(int array: arrays){
//            System.out.println(array);
//        }
        int[] reverse = reverse(arrays);
        printArray(reverse);

    }
    //Invert array
    public static int[] reverse(int[] arrays){
        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;
    }
    //Print array elements
    public static void printArray(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]+" ");
        }
    }
//result
22 
21 
17 
16 

Multidimensional array

  • Multidimensional array can be regarded as an array of arrays (two-dimensional array is a special one-dimensional array, and each element is a one-dimensional array)

Two dimensional array a can see an array of 16 rows and 21 columns
int a[][] = new int[16][21];

   public static void main(String[] args) {
        //[4][2]
        /*
        1,5 array[0]
        15,20 array[1]
        16,21 array[2]
        17,22 array[3]
         */
        int[][] array = {{1,5},{15,20},{16,21},{17,22}};
//        System.out.println(array[0][0]);
//        System.out.println(array.length);
//        System.out.println(array[0].length);
//        printArray(array[0]);
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }

        }
// result
1 5 15 20 16 21 17 22 

application

Arrays class

  • Array tool class java util. Arrays
  • There are no methods for us to call array objects, but the API provides a tool class Arrays for us to use, so we can perform some basic operations on data objects
  • View JDK help documentation
  • The methods in the Arrays class are static methods decorated with static. When using them, you can call them directly with the class name instead of using objects

It has the following common functions
Assign a value to the array: through the fill method
Sort the array: sort in ascending order through the sort method
Compare arrays: use the equals method to compare whether the element values in the array are equal
Find array elements: binary search can be performed on the sorted array through the binarySearch method

    public static void main(String[] args) {
        int[] a = {17,20,16,22};
//        System.out.println(a);
        //Print array element arrays toString
        System.out.println(Arrays.toString(a));
//        printArray(a);

        //Sort array
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        Arrays.fill(a,17);
        System.out.println(Arrays.toString(a));

    }
//result
[17, 20, 16, 22]
[16, 17, 20, 22]
[17, 17, 17, 17]

Bubble sorting

  • One of the eight sorts

The bubble code is quite simple. The two-layer cycle, the number of bubble wheels in the outer layer and the inner layer are compared in turn
Nested loop with time complexity O(n^2)

  public static void main(String[] args) {
        int[] a = {1,5,15,16,17,18,22,21};
        //After calling the sorting method written by ourselves, return a sorted array
        int[] sort = sort(a);
        System.out.println(Arrays.toString(sort));

    }
    public static int[] sort(int[] array){
        //Optimization: flag identification is used to reduce meaningless comparison
        boolean flag = false;
        //Temporary variable
        int temp = 0;
        //Outer circulation, judge how many times we have to go;
        for (int i = 0; i < array.length-1; i++) {
            //The inner loop compares and judges two numbers. If the first number is larger than the second number, the position is exchanged
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j+1]>array[j]){
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flag = true;
                }
            }
            if (flag==false){
                break;
            }
        }
        return array;
    }
//result
[22, 21, 18, 17, 16, 15, 5, 1]

Sparse array

  • When most elements in an array are 0 or an array with the same value, sparse array can be used to save the array

Sparse arrays are handled as follows:
How many rows and columns are there in the record array? How many different values are there
Record the elements, rows, columns and values with different values in a small-scale array, so as to reduce the size of the program

  public static void main(String[] args) {
        //1. Create a two-dimensional array 20 * 20 0: no chess pieces 1: black chess 2 White chess
        int[][] array1 = new int[20][20];
        array1[1][5] = 1;
        array1[15][17] = 2;
        //Output original array
        System.out.println("Output original array");
        for (int[] ints : array1) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
        System.out.println("============");
        //Convert to sparse array save
        //Get the number of valid values
        int sum = 0;
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if(array1[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("Number of valid values:"+sum);
        //2. Create an array of sparse arrays
        int[][] array2 = new int[sum+1][3];
        array2[0][0] = 20;
        array2[0][1] = 20;
        array2[0][2] = sum;
        //Traverse the two-dimensional array 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");
        }
        System.out.println("===============");
        System.out.println("reduction");
        //1. Read sparse array
        int[][] array3 = new int[array2[0][0]][array2[0][1]];
        //2. Restore its value to the element in it
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }
        //3. Printing
        System.out.println("Output restored array");
        for (int[] ints : array1) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
    }
//result

0	0	0	0	0	0	0	0	0	0	0	0	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	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	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	
============
Number of valid values: 2
 Sparse array
20	20	2	
1	5	1	
15	17	2	
===============
reduction
 Output restored array
0	0	0	0	0	0	0	0	0	0	0	0	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	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	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	

The process has ended,Exit code 0

Keywords: Java Back-end

Added by sandrol76 on Mon, 28 Feb 2022 12:29:58 +0200