Array learning of JAVA

Posted by mhodgson on Tue, 25 Jan 2022 09:19:41 +0100

Array learning of JAVA

The array has a fixed length and sets the corresponding type of container

Create array

1. Declaring arrays and creating fixed length arrays

public class Hello{
    public static void main(String[] args){
        int[] a//Declare an array
        //Create an array with a length of 5 and point to it with reference a
        a = new int[5];
         
        int[] b = new int[5]; //While declaring, point to an array
    }
}

2. Access array and array length

Array subscript base 0, subscript 0 represents the first number in the array

​ . The length property is used to access the length of an array. The array access subscript range is 0 to length - 1. Once this range is exceeded, an array subscript out of bounds exception will be generated

3. About array sorting

Select sort

Idea: compare the first position with all others. As long as it is smaller than the first position, change to the first position. After comparison, the first position is the smallest, and then compare it with the rest from the second position. As long as it is smaller than the second position, change to the second position
After comparison, the second is the second smallest

public class Hello{
	public static void main(String arg){
        int a[] = new int[]{18,62,68,82,65,9};
        for(int i=0;i<a.length;i++){
            for(int j=i+1;j<a.length;j++){
                if(a[i]<a[j){
                    int temp =a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
            }
        }
        //Print out the content
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
             
    
    }
}

bubble sort

Bubble sorting idea:
Step 1: start from the first bit and compare the two adjacent bits
If it is found that the front one is larger than the back one, exchange the large data in the back. After the cyclic comparison, the last one is the largest
Step 2: do it again, but don't compare the last one

public class Hello{
	public static void main(String arg){
        int a[] = new int[]{18,62,68,82,65,9};
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a.length-j-1;j++){
                if(a[j]<a[j+1){
                    int temp =a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        //Print out the content
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
             
    
    }
}

4. Enhanced for loop

Note: the enhanced for loop can only be used to take values, but not to modify the values in the array

public class Hello{
	public static void main(String arg){
        int a[] = new int[]{18,62,68,82,65,9};
        //General traversal
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        //Enhanced for loop traversal
        for(int val:a){
            System.out.print(val + " ");
        }
             
    
    }
}

5. Array copy

System.arraycopy(src, srcPos, dest, destPos, length)

src: source array
srcPos: the starting position for copying data from the source array
dest: target array
destPos: copy to the start of the target array
Length: the length of the copy

6. Two dimensional array

This is a one-dimensional array. Each element in it is a basic type int

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

This is a two-dimensional array. Every element in it is a one-dimensional array, so a two-dimensional array is also called an array of arrays

int b[][] = new int[][]{

{1,2,3},

{4,5,6},

{7,8,9}

};

7,Arrays

Arrays is a tool class for arrays. It can sort, find, copy, fill and other functions.

import java.util.Arrays;

public class HelloWorld {
    public static void main(String[] args) {
 		  int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
          // The first parameter represents the source array
        // The second parameter indicates the start position (obtained)
        // The third parameter indicates the end position (not available)
        int[] b = Arrays.copyOfRange(a, 0, 3);
 
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }
        //Convert to string
        String content = Arrays.toString(a);
        System.out.println(content);
        
        //The Arrays utility class provides a sort method
        System.out.println("Before sorting :");
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("After sorting:");
        System.out.println(Arrays.toString(a));
  		
        //Before using binarySearch, you must use sort to sort
        System.out.println("Where the number 62 appears:"+Arrays.binarySearch(a, 62));
        
        //Judge whether it is the same
        System.out.println(Arrays.equals(a, b));
        
        //Fill the entire array with the same value
        int c[] = new int[10];
        Arrays.fill(c, 5);
        System.out.println(Arrays.toString(c));
    }
}