Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- public class Test {
- public static void main(String[] args) {
- Object[] a1 = {1,2,3,4,5,6,7,8,9,10};
- for (int i = 0; i < a1.length; i++) {
- moveNElementsToTheEnd(a1, 1);
- System.out.println(Arrays.toString(a1));
- }
- System.out.println();
- Object[] a2 = {1,2,3,4,5,6,7,8,9,10,11,12,13};
- for (int i = 0; i < a2.length; i++) {
- moveNElementsToTheEnd(a2, 1);
- System.out.println(Arrays.toString(a2));
- }
- }
- static Object[] moveNElementsToTheEnd(Object[] initArray, int n) {
- List<Object> l = Arrays.asList(initArray);
- Collections.rotate(l, -n);
- return initArray;
- }
- }
- // [2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
- // [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]
- // [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
- // [5, 6, 7, 8, 9, 10, 1, 2, 3, 4]
- // [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
- // [7, 8, 9, 10, 1, 2, 3, 4, 5, 6]
- // [8, 9, 10, 1, 2, 3, 4, 5, 6, 7]
- // [9, 10, 1, 2, 3, 4, 5, 6, 7, 8]
- // [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- //
- // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1]
- // [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2]
- // [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3]
- // [5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4]
- // [6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5]
- // [7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6]
- // [8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7]
- // [9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8]
- // [10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- // [11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- // [12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
- // [13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
- // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement