-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseArray.java
More file actions
67 lines (56 loc) · 1.73 KB
/
ReverseArray.java
File metadata and controls
67 lines (56 loc) · 1.73 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class ReverseArray {
public static void main(String[] args) {
System.out.println("Welcome to Reverse an Array Method");
int[] MainArray = arrayInputUtility.inputArray();
int[] Result1 = ReverseArrayMethod1(MainArray.clone());
int[] Result2 = ReverseArrayMethod2(MainArray.clone());
int[] Result3 = ReverseArrayMethod3(MainArray.clone());
System.out.print("Method 1 : ");
DisplayArray(Result1);
System.out.print("Method 2 : ");
DisplayArray(Result2);
System.out.print("Method 3 : ");
DisplayArray(Result3);
}
public static int[] ReverseArrayMethod1 (int[] Array){
int[] NewArray = new int[Array.length];
int i = 0;
int j = Array.length-1;
while (i < Array.length) {
NewArray[j] = Array[i];
j--;
i++;
}
return NewArray;
}
public static int[] ReverseArrayMethod2(int[] Array){
int i = 0;
int j = Array.length-1;
while (i < j) {
int temp = Array[i];
Array[i] = Array[j];
Array[j] = temp;
i++;
j--;
}
return Array;
}
public static int[] ReverseArrayMethod3(int[] Array){
int i = 0;
while (i < Array.length / 2) {
int swap = Array[i];
Array[i] = Array[(Array.length -1)-i];
Array[(Array.length -1)-i] = swap;
i++;
}
return Array;
}
public static void DisplayArray(int[] Array) {
int i = 0;
while (i < Array.length) {
System.out.print(Array[i] + " ");
i++;
}
System.out.println();
}
}