分享一个Java经典编程题的实例

零下一度
零下一度 原创
2017-06-25 10:55:27 962浏览

有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移动" + n + "位后的数组为:");
for (int r : b) {
System.out.print(r + " ");
}
} else {
System.out.print("\n移动错误!");
}
}
}

以上就是分享一个Java经典编程题的实例的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。