-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateAndReverse.java
More file actions
33 lines (30 loc) · 933 Bytes
/
RotateAndReverse.java
File metadata and controls
33 lines (30 loc) · 933 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
28
29
30
31
32
33
public class RotateAndReverse {
/*
* Sofie Budman Period 5
*/
public String[] rotate(String[] array, int stepsToRight){
String[] out = new String[array.length];
for(int i = 0; i < array.length; i ++){
int index = (i + stepsToRight) % array.length;
out[index] = array[i];
}
return out;
}
public String[] reverse(String[] array){
String[] out = new String[array.length];
for(int i = 0; i < array.length; i ++){
out[i] = array[array.length - i - 1];
}
return out;
}
public static void main(String[] args){
RotateAndReverse r = new RotateAndReverse();
String[] a = new String[]{"a", "b", "c", "d", "e"};
for(String s : r.rotate(a, 2)){
System.out.println(s);
}
for(String s : r.reverse(a)){
System.out.println(s);
}
}
}