Use java to realize the progressive shift of Arrays

Source: Internet
Author: User

Use java to implement an array loop step by step shift operation 1. the background teacher assigned several java programming questions in the class. This is one of them. design a class, add a constructor with a parameter, and encapsulate a method. When this method is called consecutively, the following elements are output in sequence: 5 7 4 8 9 1 2 call output: 1 5 7 4 8 9 3 call output: 9 1 5 7 4 8 4 call output: 8 9 1 5 7 4 5 call output: 4 8 9 1 5 7 6 call output: 7 4 8 9 1 5 3. code (with code explanation) [java] <span style = "font-family: SimSun; font-size: 14px"> package One; /*** @ author Kun Sun * @ Date: 2013.10.15 */public class Move {// mobile class int array []; public Move (int array []) {this. array = array;} private void movebit (int array []) {// shift method int temp; temp = array [array. length-1]; for (int I = array. length-1; I> 0; I --) {array [I] = array [I-1];} array [0] = temp;} public void talk () {// encapsulate movebit (array);} public void print (int array []) {// output int I = 0; for (I = 0; I <array. length; I ++) {System. out. print (array [I] + "");} System. out. println () ;}</span> [java] <span style = "font-family: SimSun; font-size: 14px"> package One; /*** @ author Kun Sun * @ Date: 2013.10.15 */public class MainClass {// used to test the mobile class/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub int array [] = new int [] {5, 7, 4, 8, 9, 1}; Move move = new Move (array); int I = 0; while (I <6) {System. out. println ("no." + (I + 1) + "Call output:"); move. print (array); move. talk (); I ++ ;}}</span> 4. test Run result

Related Article

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.