Full introduction to Java array (2)

Posted by cs.punk on Tue, 05 May 2020 04:00:28 +0200

Example analysis

1. Pass in a value in the function and find the corresponding array corner

public class Rewriter {
    public static int fun(int num) {
        int[] a =new int[] {1,2,3,4,5};
        for (int i = 0; i < a.length; i++) {
            if (a[i] == num) {
                return i;
            }
        }
        return -1;      //Returns - 1 when the value passed in is not in the array
    }
    public static void main(String[] args) {    
        int num =fun(4);
        System.out.println(num);
    }
}
//Output:3

2. Use the same array to reverse its inner elements

public class Rewriter {
    public static void fun() {
        int[] a =new int[] { 1, 2, 3, 4, 5};
        for (int i = 0; i < a.length/2; i++) {
            int temp = a[a.length-1-i];           // Redistributing two numbers with intermediate values
            a[a.length -1-i] = a[i];
            a[i] = temp;
        }
        System.out.println(Arrays.toString(a));  // Use toString method in Arrays to traverse a array and output it as a string
    }

    public static void main(String[] args) {            
        fun();
    }
}
//Output:[5, 4, 3, 2, 1]

3. Bubbling cycle

Compare two adjacent numbers and exchange the positions of two numbers

public class Rewriter {
    public static void main(String[] args) {            
        int[] a = new int[] {5,4,3,2,1};
        for (int i = 0; i < a.length - 1; i++) {       
        //a.length -1 will be compared when two adjacent numbers are compared
            for (int j = 0; j < a.length - 1 - i; j++) {    
            //The number of internal cycles is higher than the number of times of each trip, and the number of times of each trip is higher than the number of times of the previous trip - 1    
                if (a[j] > a[j+1]) {
                    int temp = a[j+1];
                    a[j+1] = a[j];
                    a[j] = temp;
                }
            }
        }System.out.println(Arrays.toString(a));
    }
}
//Output:[1, 2, 3, 4, 5]

4. Select Sorting

Select a number and compare the exchange position with each next number, and so on

public class Rewriter {
    public static void main(String[] args) {            
        int[] a = new int[] {5,4,3,2,1};
        for (int i = 0; i < a.length - 1; i++) {                
        //The first preferred number is compared with all the following numbers in turn four times in the cycle the second three times
            for (int j = i+1; j < a.length ; j++) {  
            //The first one compared four times, the second three times             
                if (a[i] > a[j]) {
                    int temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }
        }System.out.println(Arrays.toString(a));
    }
}

5. Find the game

Randomly generate a number [1100], and the user inputs a number. If the output is larger than this number, the user will output again until the input is correct

public class Rewriter {
    public static void main(String[] args) {    
        System.out.println("Please enter a 1~100 Number within");
        Scanner scanner = new Scanner(System.in);
        int num = (int) (Math.random()*100+1);      
        while (true) {
            int n = scanner.nextInt();
            if (n > num) {
                System.out.println("You have entered a large number");
            }else if (n < num) {
                System.out.println("The number you entered is small");
            }else {
                System.out.println("Correct");
                break;
            }
        }   
    }
}

6. Half search

In an ordered array, enter an element value or a subscript to find the corresponding subscript or value

public class Rewriter {
    public static void fun(int[] a,int b) { 
        //Passing in array and element values that need to be queried
        int max = a.length - 1;     
        //Maximum angle sign
        int min = 0;                
        //Minimum angle sign
        int mid = (max + min) / 2;  
        //Middle angle sign
        while (b != a[mid]) {
            if (b > a[mid]) {   
                //When the angle sign to be searched is larger than the middle value
                min = mid + 1;
            }else if (b < a[mid]) { 
                //When the angle scale to be searched is smaller than the middle value
                max = mid -1;
            }
            mid = (max + min) / 2;  
            //Redefining intermediate values
            if (max < min) {        
                //When the maximum angle sign is less than or equal to the minimum angle sign, the value does not exist
                mid = -1;
            }
        }
        System.out.println(mid);
    }

    public static void main(String[] args) {    
        int[] a = new int[] {1,5,10,15,20,25,30,35};
        fun(a, 30);
    }
}

Topics: less