Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Integer[] arr = {1,2,3,4,5};
- Collections.rotate(Arrays.asList(arr), -3);
- System.out.println(Arrays.toString(arr));
- [4, 5, 1, 2, 3]
- int [] a = {1,2,3,4,5};
- int [] b = new int[a.length];
- Arrays.setAll(b, (int i) -> a[(i+3)%a.length]);
- System.out.println("a="+Arrays.toString(a));
- System.out.println("b="+Arrays.toString(b));
- a=[1, 2, 3, 4, 5]
- b=[4, 5, 1, 2, 3]
- int[] buffer = new int[placesToShift];
- // Save the start of the array in the buffer
- System.arraycopy(array, 0, buffer, 0, placesToShift);
- // Copy the rest of the array into place
- System.arraycopy(array, placesToShift, array, 0, array.length - placesToShift);
- // Copy the buffer into the end
- System.arraycopy(buffer, 0, array, array.length - placesToShift, buffer.length);
- for (int i = 0; i < placesToShift; i++) {
- int firstElement = array[i];
- System.arraycopy(array, 1, array, 0, array.length - 1);
- array[array.length - 1] = firstElement;
- }
- final int first = 0;
- int currIndex = first;
- int temp = a[currIndex];
- do {
- int nextIndex = (currIndex + 3) % a.length;
- a[currIndex] = (nextIndex == first) ? temp : a[nextIndex];
- currIndex = nextIndex;
- } while (currIndex != first);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement