java learning notes

java learning notes (7)

Start learning java and form the habit of blogging. I hope I can stick to it. Small partners with similar interests can learn together java Foundation

22, Array

Definition of array

  • An array is an ordered collection of data of the same type
  • Array describes several data of the same type, which are arranged and combined in a certain order
  • Each of these data is called an array element, and each array element can access it through a subscript

Array declaration creation

package mifan.array;

public class ArrayDemo01 {
    //Type of variable name of variable = value of variable
    //Array type
    public static void main(String[] args) {
        int[] nums;//array define
        //int nums2[];// Early in order to make c programmers adapt to java

        nums = new int[100];

        //Assign values to array elements
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;

        //Calculate the sum of all elements
        int sum = 0;
        //Get the array length array The length is determined by the size of the array when it is created
        for (int i = 0; i < nums.length; i++){
            sum += nums[i];
        }
        System.out.println("The sum is"+sum);
    }
}

Memory analysis

Three kinds of initialization

package mifan.array;

public class ArrayDemo02 {
    public static void main(String[] args) {
        //initiate static
        int[] a = {1,2,3,4,5,6,7,8};
        //Dynamic initialization, including default initialization
        int[] b = new int[10];
        b[0] = 10;
    }
}

Four characteristics of array

  • The length of the array is fixed. Once created, the size cannot be changed
  • Array elements must be of the same type. Mixed types are not allowed
  • The elements in the array can be any data type, including basic type and reference type
  • Array variables are of reference type. An 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, and the objects in java are in the heap. Therefore, whether the array saves the original type or other object types, the array object itself is in the heap.

Array boundary

package mifan.array;

public class ArrayDemo02 {
    public static void main(String[] args) {
        //initiate static
        int[] a = {1,2,3,4,5,6,7,8};
        //Subscript out of bounds
        for (int i = 0; i <= a.length; i++) {
            System.out.println(a[i]);
        }

    }
}

Use of arrays

package mifan.array;

public class ArrayDemo03 {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};

        //Print all array elements
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println("***************");

        //Calculate the sum of all elements
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        System.out.println(sum);
        System.out.println("***************");
        //Find maximum element
        int max  = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i]>max){
                max = array[i];
            }
        }
        System.out.println(max);
    }
}

package mifan.array;

public class ArrayDemo04 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};
       // printArrays(arrays);
        int[] resverse = reverse(arrays);
        printArrays(resverse);
        //For each loop jdk1 The subscript is omitted for versions above 5
//        for (int array : arrays) {
//            System.out.println(array);
//        }
    }
        //Array as method input parameter
    public static void printArrays(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]);
        }
    }

    //Invert array
    public static int[] reverse(int[] arrays){
        int[] result = new int[arrays.length];
        //Reverse operation
        for (int i = 0,j=result.length-1; i < arrays.length; i++,j--){
            {
                result[j]=arrays[i];
            }
        }
        return result;
    }




}

Multidimensional array

package mifan.array;
//Multidimensional array
public class ArrayDemo05 {
    public static void main(String[] args) {
        int[][] array = {{1,2},{2,3},{3,4},{4,5}};
        //printArrays(array[0]);
       // System.out.println(array[0][1]);
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]);
            }
            System.out.println();
        }
    }
//Print
    public static void printArrays(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]);
        }
    }
}


Arrays class

package mifan.array;

import java.util.Arrays;

public class ArrayDemo06 {
    public static void main(String[] args) {

        int[] a = {1,2,3,4,990011,312454,54646,1213};
        Arrays.sort(a);//Ascending sort

        //Print array elements
        System.out.println(Arrays.toString(a));

        //fill
        Arrays.fill(a,2,4,0);// Fill the [2,4) position in the a array with 0,
        System.out.println(Arrays.toString(a));
    }
}

Bubble sorting

package mifan.array;

import java.util.Arrays;

public class ArrayDemo07 {
    public static void main(String[] args) {
        int[] a = {2,4,23,345,6,7,324};

        System.out.println(Arrays.toString(sort(a)));
    }

    public static int[] sort(int[] array){
        int temp = 0;
        //Outer circle, judge how many times we have to go
        for (int i = 0; i < array.length-1; i++) {

            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;
                }
            }
        }
        return array;
    }
}

Sparse array

  • When most elements in an array are 0 or an array with the same value, you can use a sparse array 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 of specific different values in a small-scale array, so as to reduce the scale of the program
package mifan.array;

public class ArrayDemo08 {
    public static void main(String[] args) {
        //1. 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();
        }

        //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("The number of valid elements is:"+sum);

        //Create a sparse array
        int[][] array2 = new int[sum+1][3];

        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;

        //Traverse the two-dimensional array and store the non-zero value in the sparse matrix
        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");
        }

    }
}

Every bully has a hard time. As long as he can stick to it like a fool, he will be bullied in the end!

Keywords: Java

Added by dimxasnewfrozen on Mon, 28 Feb 2022 16:26:51 +0200