-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySwap.java
More file actions
29 lines (26 loc) · 968 Bytes
/
ArraySwap.java
File metadata and controls
29 lines (26 loc) · 968 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
import java.io.*;
public class ArraySwap {
public static void main(String[] args) throws IOException{
//create our buffer
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
//prompt the user
System.out.println("Enter 10 numbers separated by spaces");
//put our input values into an array
String[] inputValues = bufferedReader.readLine().split(" ");
//need two 'pointers' one to track elements from the front of the array
//and another to track elements from the end of the array
//then swap the values
for (int i = 0, j = inputValues.length - 1; i < j; i++, j--){
//temp variable
String temp = inputValues[j];//start with whatever is at the back
inputValues[j] = inputValues[i];
inputValues[i] = temp;
}
//for (int x=0; x<inputValues.length; x++){
// System.out.println("arr["+ x + "]= " + inputValues[x]);
//}
for (String value: inputValues) {
System.out.println(value);
}
}
}