zoukankan      html  css  js  c++  java
  • Java经典编程题50道之三十六

    有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。

    public class Example36 {
        public static void main(String[] args) {
            int[] m = { 18, 12, 23, 34, 95, 76, 57, 28, 9 };
            moveElement(m, 5);
        }

        public static void moveElement(int[] m, int n) {
            System.out.print("移位前的数组为:");
            for (int r : m) {
                System.out.print(r + " ");
            }

            if (n <= m.length) {
                int[] b = new int[m.length];
                for (int i = 0; i < m.length - n; i++) {
                    b[i + n] = m[i];
                }
                int j = 0;
                for (int i = m.length - n; i < m.length; i++) {
                    b[j] = m[i];
                    j++;
                }

                System.out.print(" 移动" + n + "位后的数组为:");
                for (int r : b) {
                    System.out.print(r + " ");
                }
            } else {
                System.out.print(" 移动错误!");
            }
        }
    }

  • 相关阅读:
    java的多线程学习,第二记
    java多线程的学习
    长城
    2018-12-6
    mysql的笔记
    springboot用jpa生成表,没有外键
    idea 使用方法
    Oracle数据库中文乱码问题
    JAVA-Could not create the Java virtual machine java启动失败
    log4j日志如何在ssh中配置?
  • 原文地址:https://www.cnblogs.com/qubo520/p/6962529.html
Copyright © 2011-2022 走看看