The system function mentioned above, arrycopy, is a powerful function. According to its characteristics, it can be seen that due to its special properties, if it is used, elements will be added to the array, but the result of this method is, If n elements are added, the last n elements in the original array will be lost.
For example, adding elements to an array:
1 // Add an element to an array. (Solution: In order to add an element at the i-th position, you can first move all the elements starting from the i-th position backward by one position, and then insert the new value. Note that the last element of the original array will be lost.) 2 3 // import the package required for input 4 import java.util.Scanner; 5 6 public class test5 7 { 8 public static void main(String [] arys) 9 { 10 int [] arr1 = {1,2,3,4 ,5 }; 11 12 Scanner sc = new Scanner(System.in); // Create an input stream object 13 System.out.println("Please enter the position to be inserted: " ); 14 int index = sc.nextInt(); // Get the integer value entered by the user 15 16 System.out.print("Please enter the value you want to insert:" ); 17 int num = sc.nextInt(); 18 19 // Implement data insertion 20 // 1. Move the value starting from the specified position back. Since the length of the array is immutable, the last number will be lost . 21 System.arrycopy(arr1,index,arr1,index+1,arry.length-index-1 ); 22 23 // 2. Add data to the specified position 24 arr1[index] = num; 25 26 for ( int n : arr1) 27 { 28 System.out.print(n+"\t"); 29 } 30 } 31 }