1 Public Static voidMain (string[] args) {2 //insert a data in an array [step]----Premise: The array is an ordered array!!! 3 //01. First declare this array4 //the first array5 int[] num = {10, 25, 36, 49 };6 //traversing output num array elements7 for(inti = 0; i < num.length; i++) {8System.out.print (Num[i] + "\ T");9 }Ten System.out.println (); One //02. Create a new array with an array length of the original array length + number of digits you want to insert A //defines a new array nums - int[] Nums =New int[5]; - //03. Declare the number you want to insert the //define an integer number to insert - intNumber =-5; - //04. Assigning the original array to a new array - //assigns the first array with length 4 to a new array of length 5 + for(inti = 0; i < num.length; i++) { -Nums[i] =Num[i]; + } A //05. Output The elements of the new array (it will be found that the new array has a value of 0 on one or more positions than the original array) at //iterate through the elements of the new array output - for(inti = 0; i < nums.length; i++) { -System.out.print (nums[i] + "\ T"); - } - System.out.println (); - //06. Declare a location where the number you want to insert occupies a position on the new array in //defines a subscript that represents the position of number in the array nums - intindex = nums.length-1; to //07. Find this position through a circular comparison + for(inti = 0; i < nums.length; i++) { - if(Nums[i] >Number ) { theindex =i; * Break; $ }Panax Notoginseng } - //08. Change the position of other elements before or after this position to forward or backward the for(inti = nums.length-1; i > Index; i--) { +Nums[i] = nums[i-1]; A } the //09. Assign the number you want to insert to the element of the new array on the found position +Nums[index] =Number ; - //10. Iterate through the elements in the output new array--------Insert complete!!! $ for(inti = 0; i < nums.length; i++) { $System.out.print (nums[i] + "\ T"); - } -}
Array (insert algorithm [int])