-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathReverseArrayTest.java
More file actions
50 lines (43 loc) · 1.66 KB
/
ReverseArrayTest.java
File metadata and controls
50 lines (43 loc) · 1.66 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
/**
* Write a method called reverseArray() with the following signature:
*
* public static void reverseArray(int[] intArray)
*
* The method accepts an int array, and reverses its orders.
* For example, if the input array is {12, 56, 34, 79, 26},
* the reversal is {26, 79, 34, 56, 12}.
*
* 1. You MUST NOT use another array in your method (but you need a temporary
* variable to do the swap).
*
* 2. Take note that the array passed into the method can be modified by the method
* (this is called "pass by reference"). On the other hand, primitives passed
* into a method cannot be modified. This is because a clone is created and
* passed into the method instead of the original copy (this is called "pass by value").
*/
package javaexercises.methods;
public class ReverseArrayTest {
public static void main(String[] args) {
ReverseArrayTest aReverseArrayTest = new ReverseArrayTest();
int[] intArray = {12, 56, 34, 79, 26, 100};
aReverseArrayTest.printArray(intArray);
aReverseArrayTest.reverseArray(intArray);
aReverseArrayTest.printArray(intArray);
}
private void reverseArray(int[] intArray) {
if (intArray.length == 0) {
return;
}
for(int i = 0; i < intArray.length / 2; i++) {
int tmp = intArray[i];
intArray[i] = intArray[intArray.length - i -1];
intArray[intArray.length - i - 1] = tmp;
}
}
private void printArray(int[] intArray) {
for(int i = 0; i < intArray.length; i++) {
System.out.printf("%d ", intArray[i]);
}
System.out.println();
}
}