-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCopyReverseArrays.java
More file actions
35 lines (27 loc) · 883 Bytes
/
CopyReverseArrays.java
File metadata and controls
35 lines (27 loc) · 883 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
34
35
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// write testcode here
int[] original = {1, 2, 3, 4};
int[] reverse = reverseCopy(original);
// print both
System.out.println( "original: " +Arrays.toString(original));
System.out.println( "reversed: " +Arrays.toString(reverse));
}
public static int[] copy(int[] array){
int[] copy = new int[array.length];
for (int i = 0; i < array.length; i++){
copy[i] = array[i];
}
return copy;
}
public static int[] reverseCopy(int[] array){
int[] reversed = new int[array.length];
int j = 0;
for(int i = array.length - 1; i >= 0; i--){
reversed[j] = array[i];
j++;
}
return reversed;
}
}