Array (insert algorithm [int])

Source: Internet
Author: User

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])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.