-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathArrayIntReverse.java
More file actions
49 lines (36 loc) · 852 Bytes
/
ArrayIntReverse.java
File metadata and controls
49 lines (36 loc) · 852 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Scanner;
public class ArrayIntReverse {
public static int[] read(int n) {
int[] a = new int[n];
int N = a.length;
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < N; i++) {
a[i] = scanner.nextInt();
}
scanner.close();
return a;
}
public static int[] reverse(int[] a) {
int N = a.length;
int[] b = new int[N];
for (int i = 0; i < N / 2 + 1; i++) {
b[i] = a[N - i - 1];
b[N - i - 1] = a[i];
}
return b;
}
public static void print(int[] a, int[] b) {
System.out.print("\nInitial:\n");
for (int num : a)
System.out.print(num + " ");
System.out.print("\nReversed:\n");
for (int num : b)
System.out.print(num + " ");
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int[] a = read(n);
int[] b = reverse(a);
print(a, b);
}
}