Move implementation of element position in array

Source: Internet
Author: User

Title: There are n integers, so that they move backward in the order of the first number of M positions, the last m number becomes the first number of M

Analysis: Implementation of the move is actually the operation of the index, the array element has not changed, and the index value has changed,

Rational use of% operation, original array index%len= index of original array--(original array index + move number)%len= new array index

     Public Static voidFunction01 () {int[] array={2,3,4,6,7,9}; //copy an array for storing the data        int[] arraycopy=arrays.copyof (array, array.length); System.out.println ("Original array:");        System.out.println (arrays.tostring (array)); System.out.println ("Please enter the number of digits to move:"); Scanner SC=NewScanner (system.in); intMovenum=sc.nextint ();//number of bits moved                 for(inti = 0; i < arraycopy.length; i++) {            intMove= (i+movenum)%Array.Length; Arraycopy[move]=Array[i];                    } System.out.println (Arrays.tostring (arraycopy)); }

Original array:
[2, 3, 4, 6, 7, 9]
Please enter the number of digits to move:
3
[6, 7, 9, 2, 3, 4]

Move implementation of element position in array

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.