-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShiftArrayValues.java
More file actions
27 lines (22 loc) · 904 Bytes
/
ShiftArrayValues.java
File metadata and controls
27 lines (22 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.java.test;
import java.util.Arrays;
public class ShiftArrayValues {
public static void main(String[] args) {
int[] arrayNumbers = {5, 3, 4, 6, 10};
Arrays.stream(arrayNumbers).forEach(x -> System.out.print(x + " "));
System.out.println("");
int movement = 2;
int[] tmpArrayNumbers = arrayNumbers.clone();
// Arrays.stream(tmpArrayNumbers).forEach(x -> System.out.print(x + " "));
System.out.println("");
if (movement > arrayNumbers.length)
movement = movement % arrayNumbers.length;
for(int x = 0; x < arrayNumbers.length; x++) {
if (x + movement < arrayNumbers.length)
System.arraycopy(arrayNumbers, x, tmpArrayNumbers, x + movement, 1);
else
System.arraycopy(arrayNumbers, x, tmpArrayNumbers, (x + movement) - arrayNumbers.length, 1);
}
Arrays.stream(tmpArrayNumbers).forEach(x -> System.out.print(x + " "));
}
}