-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverselyPrint.java
61 lines (48 loc) · 1.54 KB
/
ReverselyPrint.java
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
import java.util.*;
class ReverselyPrint {
public static void main(String[] args) {
int[] numbers = readArray();
reversePrint(numbers);
}
public static int[] readArray() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of integers: ");
int numInt = sc.nextInt();
int[] values = new int[numInt];
System.out.print("Enter the " + numInt + " integers: ");
for(int i = 0; i < numInt; i++) {
values[i] = sc.nextInt();
}
sc.close();
return values;
}
public static int getElement(int[] arrInt, int index) {
return arrInt[index];
}
public static void reversePrint(int[] arr) {
// changed the normal implementation from only printing to actually computing the resulting reversed array (for modifiability purposes).
// normal implementation
/*
for(int i = arr.length - 1; i > -1; i--) {
System.out.print(arr[i] + " ");
}
System.out.println();
*/
// array-forming implementation
int[] reversedArr = new int[arr.length];
int j = arr.length;
for(int i = 0; i < arr.length; i++) {
reversedArr[j - 1] = arr[i];
j--;
}
/*
int i = 0;
while(i < reversedArr.length) {
System.out.print(reversedArr[i] + " ");
i++;
}
System.out.println();
*/
System.out.println(Arrays.toString(reversedArr));
}
}