Advertisement
Guest User

Untitled

a guest
Jul 7th, 2010
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4.  
  5. public class Test {
  6.     public static void main(String[] args) {
  7.         Object[] a1 = {1,2,3,4,5,6,7,8,9,10};
  8.         for (int i = 0; i < a1.length; i++) {
  9.             moveNElementsToTheEnd(a1, 1);
  10.             System.out.println(Arrays.toString(a1));
  11.         }
  12.         System.out.println();
  13.         Object[] a2 = {1,2,3,4,5,6,7,8,9,10,11,12,13};
  14.         for (int i = 0; i < a2.length; i++) {
  15.             moveNElementsToTheEnd(a2, 1);
  16.             System.out.println(Arrays.toString(a2));
  17.         }
  18.     }
  19.    
  20.     static Object[] moveNElementsToTheEnd(Object[] initArray, int n) {
  21.         List<Object> l = Arrays.asList(initArray);
  22.         Collections.rotate(l, -n);
  23.         return initArray;
  24.     }
  25. }
  26.  
  27. // [2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
  28. // [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]
  29. // [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
  30. // [5, 6, 7, 8, 9, 10, 1, 2, 3, 4]
  31. // [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
  32. // [7, 8, 9, 10, 1, 2, 3, 4, 5, 6]
  33. // [8, 9, 10, 1, 2, 3, 4, 5, 6, 7]
  34. // [9, 10, 1, 2, 3, 4, 5, 6, 7, 8]
  35. // [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  36. // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  37. //
  38. // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1]
  39. // [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2]
  40. // [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3]
  41. // [5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4]
  42. // [6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5]
  43. // [7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6]
  44. // [8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7]
  45. // [9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8]
  46. // [10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  47. // [11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  48. // [12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  49. // [13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  50. // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement