Insertion, search, storage and coverage of arrays in Java

1. What array?

Example: Array is a type of collection used to store the same type of data

2. Storage space (stack space and heap space)

Insert picture description here

Three, code and renderings

1. Use arrays to find the maximum and minimum

Case: Accept 10 integers from the keyboard and find the maximum and minimum values.
The code is as follows (example):

import java.util.Scanner;

public class Work1 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入十个数字:");
        int[] arr =new int[10];
        for (int a = 0; a <arr.length ; a++) {
    
    
            arr[a]=sc.nextInt();
        }
        int  max=arr[0];
        int  min=arr[0];
        for (int i = 1; i <arr.length ; i++) {
    
    
            if (max<=arr[i]){
    
    
                max=arr[i];
            }
        }
        for (int j = 1; j <arr.length ; j++) {
    
    
            if (min>=arr[j]){
    
    
                min=arr[j];
            }
        }
        System.out.println("最大值为:"+max);
        System.out.println("最小值为:"+min);
    }
}

Insert picture description here

2. Finding elements in the array

Case: Input 10 numbers, save them in an array, search for a number in the array, and give information about whether it is found, if it is found, output the position of the number in the array, if it cannot find the output "cannot be found" "The
code is as follows (example):

import java.util.Scanner;

public class Work2 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入10个数:");
        int[] arr=new int[10];
        for (int i = 0; i <arr.length ; i++) {
    
    
            arr[i]=sc.nextInt();
        }
        System.out.println("请输入你想查找的数:");
        int number = sc.nextInt();
        boolean isnumber=false;
        for (int j = 0; j <arr.length ; j++) {
    
    
            if (arr[j]==number){
    
    
                System.out.println("该数处于数组中第"+(j+1)+"个位置");
                isnumber=true;
            }
        }
        if (isnumber==false){
    
    
            System.out.println("找不到");
        }
    }
}

Insert picture description here

3. Input storage of data in the array

Case: Input 10 pieces of data and store them in the array, and output the array
code as follows (example):

import java.util.Arrays;
import java.util.Scanner;

public class Work2 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入10个数:");
        int[] arr=new int[10];
        for (int i = 0; i <arr.length ; i++) {
    
    
            arr[i]=sc.nextInt();
        }
        System.out.println(Arrays.toString(arr));

Insert picture description here

4. Output the position of the subscript in the array according to the size of the input data

Case: Define a sequence of ordinal numbers {1,5,20,30,80} and ask the user to enter a number. What is the subscript to determine that the number should be inserted into the array?
The code is as follows (example):

import java.util.Scanner;
public class Work7 {
    
    
    public static void main(String[] args) {
    
    
        int[] arr={
    
    1,5,20,30,80};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字:");
        int number = sc.nextInt();
        //找到第一个比number大的数
        for (int i = 0; i< arr.length; i++) {
    
    
            if (arr[i]>number){
    
    
                System.out.println("该数应该插入的下标是:"+i);
                break;
            }
        }
    }
}

Insert picture description here

5. The specified position of the element in the array is overwritten back

Case: Define an array with a length of 8, requiring the first 7 elements to be assigned values, and the last one without assigning values. Starting from the fifth element, the following elements are sequentially overwritten, and the final result is output.
The code is as follows (example):

package work1;

import java.util.Scanner;
public class Work8 {
    
    
    public static void main(String[] args) {
    
    
        fun1();
        fun();
    }
    //方法1
    private static void fun1() {
    
    
        int [] arr={
    
    10,20,30,40,50,60,70,0};
        // i 第五个元素的下标为4 最后一个数的下标为len-1    len-2赋值给len-1  len3赋值给len-2   下标4赋值给下标5
        for (int i = arr.length-2; i >= 4; i--) {
    
    
            arr[i+1]=arr[i];
        }
        //输出数组
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.println(arr[i]+" ");
        }
    }
    //方法2
    private static void fun() {
    
    
        int[] arr=new int[8];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入七个数值:");
        for (int i = 0; i <arr.length-1 ; i++) {
    
    
            arr[i]=sc.nextInt();
        }
        for (int j = arr.length-1; j >=5 ; j--) {
    
    
            arr[j]=arr[j-1];
        }
        for (int a:arr){
    
    
            System.out.print(a+"\t");
        }
    }
}

Insert picture description here
Insert picture description here

6. Insert the specified value at the specified position in the array

The code is as follows (example):

package work1;

import java.util.Arrays;
import java.util.Scanner;
//.定义一个有序数列,{1,5,20,30,80},
// 要求用户输入一个数字,然后插到数组中,并保持升序,不能使用冒泡排序。
public class Work9 {
    
    
    public static void main(String[] args) {
    
    
        fun2();
        fun();

    }
    //方法一:
    private static void fun2() {
    
    
        int [] arr={
    
    10,20,30,40,50,60,70,0};
        int x=50;//插入的数字值
        //1.找到应该插入的下标index;
        int index=arr.length-1;//默认值就是最后一个,找第一个比x大的下标
        for (int i = 0; i < arr.length; i++) {
    
    
            if (arr[i]>x){
    
    
                index=i;
                break;
            }
        }
        System.out.println(index);
        //2.从index开始,依次向后移位
        for (int i = arr.length-2; i >=index ; i--) {
    
    
            arr[i+1]=arr[i];
        }
        //3.用x替换arr[index]
        arr[index]=x;
        //输出数组
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i]+" ");
        }
    }
    //方法2
    private static void fun() {
    
    
        int[] arr={
    
    1,5,20,30,80};
        int[] arr1=new int[6];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要插入的数字:");
        int number = sc.nextInt();
        for (int i = 0; i <arr.length ; i++) {
    
    
            arr1[i]=arr[i];
        }
        int xia=arr1.length-1;
        for (int a = arr.length-1; a >=0 ; a--) {
    
    
            if (arr[a]>number){
    
    
                xia=a;
            }
        }
        for (int b = xia; b <arr.length ; b++) {
    
    
            arr1[b+1]=arr[b];
        }
        arr1[xia]=number;
        arr=arr1;
        System.out.println(Arrays.toString(arr));
    }
}


Insert picture description here
Insert picture description here

7. Reverse output of array elements in the array

Case: 3. Output the elements in an array in reverse order, that is, the first element is exchanged with the last element, and the second number is exchanged with the penultimate element..., for example: the original array is: 9 2 5 7 8, reverse The following array is: 8 7 5 2 9 The
code is as follows (example):

package work1;

import java.util.Scanner;
public class Work3 {
    
    
    public static void main(String[] args) {
    
    
        int[] arr=new int[5];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入5个数组成原数组:");
        for (int i = 0; i <arr.length ; i++) {
    
    
            arr[i]=sc.nextInt();
        }
        int a=0;
        for (int j = 0; j <arr.length/2; j++) {
    
    
            a=arr[j];
            arr[j]=arr[4-j];
            arr[4-j]=a;
        }
        System.out.println("逆序后的新数组为:");
        for (int b:arr){
    
    
            System.out.print(b+"\t");
        }
    }
}

Insert picture description here

to sum up

The above is the content of the array application, mainly about the maximum and minimum values ​​of the array, the insertion of elements at a specified position, and the subsequent coverage of elements.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/110621454