Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. Integer[] arr = {1,2,3,4,5};
  2. Collections.rotate(Arrays.asList(arr), -3);
  3. System.out.println(Arrays.toString(arr));
  4.  
  5. [4, 5, 1, 2, 3]
  6.  
  7. int [] a = {1,2,3,4,5};
  8. int [] b = new int[a.length];
  9. Arrays.setAll(b, (int i) -> a[(i+3)%a.length]);
  10. System.out.println("a="+Arrays.toString(a));
  11. System.out.println("b="+Arrays.toString(b));
  12.  
  13. a=[1, 2, 3, 4, 5]
  14. b=[4, 5, 1, 2, 3]
  15.  
  16. int[] buffer = new int[placesToShift];
  17. // Save the start of the array in the buffer
  18. System.arraycopy(array, 0, buffer, 0, placesToShift);
  19. // Copy the rest of the array into place
  20. System.arraycopy(array, placesToShift, array, 0, array.length - placesToShift);
  21. // Copy the buffer into the end
  22. System.arraycopy(buffer, 0, array, array.length - placesToShift, buffer.length);
  23.  
  24. for (int i = 0; i < placesToShift; i++) {
  25. int firstElement = array[i];
  26. System.arraycopy(array, 1, array, 0, array.length - 1);
  27. array[array.length - 1] = firstElement;
  28. }
  29.  
  30. final int first = 0;
  31. int currIndex = first;
  32. int temp = a[currIndex];
  33. do {
  34. int nextIndex = (currIndex + 3) % a.length;
  35. a[currIndex] = (nextIndex == first) ? temp : a[nextIndex];
  36. currIndex = nextIndex;
  37. } while (currIndex != first);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement