Array loop Movement (entertainment code)

Source: Internet
Author: User
Package com. demo. sw. test;/***** move the number in an array cyclically: * For example, number array: 1, 2, 3, 4, 5, 6, 7 * move the three digits forward and the result is: *, 6, 7, 1, 2, 3 * @ author Mr. J **/public class ArrayMove {public static void main (String [] args) {int [] arr = new int [12]; // initialize the array to be moved (int I = 0; I <arr. length; I ++) {arr [I] = I + 1;} int num = 17; // number of digits to move moveNums (arr, num ); // call the method to be moved // display the moving result System. out. println ("Call methods with returned values:"); for (int I = 0; I <arr. length; I ++) {System. out. print (arr [I] + "");} System. out. println ();}/** the method needs to be passed into the array to be moved and the moving displacement */public static int [] moveNums (int [] arr, int num) {if (num> arr. length) {// If the moving displacement is greater than the length of the array, the number of turns (num = num % arr. length;} // declare an array to store the number int [] copy = new int [num] That has been moved before the array; // store the numbers to be moved in the original array for (int I = 0; I <copy. length; I ++) {copy [I] = arr [I];} // move the array for (int I = 0; I <arr. length-num; I ++) {arr [I] = arr [I + num];} // After moving, append the original number to the back of the array for (int I = 0; I <copy. length; I ++) {arr [arr. length-num + I] = copy [I];} return arr ;}}

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.