Basis of array

        For the first time, if there is any misunderstanding, please contact the blogger. All the learning content comes from java crazy God

Definition of array:

         1. Arrays are ordered collections of the same type  

         2. Each data element is called an array element, and each array element can be accessed through subscript

Declaration and creation of arrays:

         1. The subscript of the array starts from 0

        2. The length of the array is arrays.length (arrays is the name of the array)

int [] a;
int a [] ;


int[] a =new int[8];Indicates that the array can hold 8 elements
 Assign values to arrays
        a[0]=2;
        a[1]=3;
        a[2]=4;
        a[3]=5;
        a[4]=6;
        a[5]=6;
        a[6]=7;
        a[7]=8;
According to the fact that the array starts with subscript 0, we can output its corresponding value
System.out.println(a[0]);



Three initializations of arrays:

        1. Static initialization

int[] a={1,2,3};
Man []man={new man{1,2},new man{3,4}};

        2. Dynamic initialization

int [] a=new a[3];
a[0]=1;
a[1]=2;
a[2]=3

        3. Default initialization

                If there is no assignment to him, he will assign the default value according to the type you define. The default value of int is 0, so all the elements in the output array are 0

int [] a=new a[2];

Memory analysis in array

        Heap: used to store new objects and data. It can be shared by all threads and will not store other object references.

        Stack: used to store the basic variable type (including the specific value of the basic type) and the variable of the reference object (which will store the specific address of the reference in the heap)

        Method area: contains class and static variables (which can be shared by all threads)

        

  Basic characteristics of array:

        1. The length of the array is determined. Once defined, it cannot be changed.

        2. The types of elements in the array are the same

        3. Array can store any data type, including basic data type and reference data type

        4. Array variables are of reference type. Arrays can be regarded as objects. Each element in the array can be regarded as a member variable in the object. The array itself is an object. Java objects are in the heap, so the array is also in the heap.

Length of array:

        The interval of array length is [0,length-1].

         If this length is exceeded, an exception will be generated: ArrayIndexOutOfBoundsException (subscript out of bounds exception)

       

  int[] a =new int[2];
  System.out.println(a[2]);

        

Traversal array:

        1. Common methods

int []b =new b[8];
for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }

        2. Use enhanced for loop

        Here, a represents the name of the array. Defining an int type is used to represent its subscript

int []a ={1,32,43,5,2};
for (int x:a){
            System.out.println(x);
        }

  Common methods of array:

        1. Used to output array arrays.toString()

 int []a ={11,2,3,4,5,6,1234};
        //Print array elements
        System.out.println(Arrays.toString(a));
        

        2. Assign arrays.fill() to the array

 int []a ={11,2,3,4,5,6,1234};
        Arrays.fill(a,0);
        //Fill in the number between 2 and 4
        Arrays.fill(a,2,4,1);
        System.out.println(Arrays.toString(a));

        3. Sort the array   arrays.sort()       

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

For more methods, please check the JDK help document

Bubble sort:  

 public static void main(String[] args) {
        //Bubble sorting

        int []a={123,2313,53,657,867987};
        int[] array = array(a);
        System.out.println(Arrays.toString(array));
    }
    public static int[] array(int [] a){
        //Define an intermediate quantity
        int tmp=0;
        //First, the first step is the outer cycle. Why should we reduce it by one? It's because the last time it bubbles, there's no need to compare it
        for (int i = 0; i < a.length-1; i++) {
            //If the simplified operation is already in order
            boolean tape=false;
            //The inner loop is for comparison. Why should i be reduced? Because if there is no comparison, there will be one less comparison
            for (int j = 0; j < a.length-1-i; j++) {
                if(a[j]>a[j+1]){
                    tmp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=tmp;
                    //If you come in, it will become
                    tape=true;
                }
            }
            if(tape==false){
                break;
            }
        }
        return a;
    }

Keywords: Java

Added by jess3333 on Mon, 08 Nov 2021 23:16:45 +0200