Today I learned Java arrays and wrote the insertion and deletion of arrays. I am a novice, and I will write them to novices who don’t know how to do it. Please ignore them if there is any mistake. Please point them out if you have any mistakes;
/**Specify the location of the array to the array*/import java.util.*;public class ArrayInsert{ public static void main(String []args){ System.out.println("Please enter 5 numbers with the keyboard: "); int [] array =new int[10]; Scanner sc=new Scanner(System.in); //Enter the number to the array through the keyboard for(int i=0;i<array.length-5;i++){ array[i]=sc.nextInt(); } //Tranquility through the array System.out.print("The original array is:"); for(int a:array){ System.out.print(" "+a); } //Insert the number of System.out.println("/nPlease enter the insertion position: the valid position is 0-----"+(array.length-1)); int index=sc.nextInt(); System.out.println("/nPlease enter the inserted value-----"); int num=sc.nextInt(); //Call the static function index //Transweep the inserted array System.out.println("Array traversal after inserting element:"); Insert(index,num,array); for(int i=0;i<array.length;i++){ System.out.print(" "+array[i]); } } //Insert data method to the specified position of the array public static int[] Insert(int index,int num,int a[]){ //If there is an element, the element after the index is moved one by one, for(int a[i]=a[i-1]; } a[index]=num; return a; }} //Delete the number at the specified position of the array. import java.util.*;public class ArrayDelete{ public static void main(String args[]){ System.out.println("Please enter 5 numbers with the keyboard: "); int [] array =new int[10]; Scanner sc=new Scanner(System.in); //Enter the number into the array through the keyboard for(int i=0;i<array.length-5;i++){ array[i]=sc.nextInt(); } //Transweep the array System.out.print("The original array is:"); for(int a:array){ System.out.print(" "+a); } //Delete the number in the specified position System.out.println("/n Enter the location you want to delete: The range is 0---"+(array.length-1)); int index=sc.nextInt(); delete(index,array);//Call delete method//Transip after deletion System.out.println("Transip after deletion:"); for(int i=0;i<array.length;i++){ System.out.print("+array[i]); } } public static int[] delete(int index,int array[]){ //Transip after deletion index, move the back of the array one by one for(int i=index;i<array.length;i++){ if(i!=array.length-1){ array[i]=array[i+1]; }else{//handle the last bit beyond the situation array[i]=array[i]; } } return array; }}The above example code for inserting and deleting specified elements in arrays in JAVA is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.