-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swapelements.java
30 lines (27 loc) · 1.11 KB
/
Swapelements.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
import java.util.Arrays;
public class Swapelements {
public static String[] swapElements(String line) {
String[] numArray = line.substring(0, line.indexOf(":")).split(" ");
String[] swapArray = line.substring(line.indexOf(":") + 1).split(",");
for (int i = 0; i < swapArray.length; ++i) {
String[] swaps = swapArray[i].split("-");
Integer firstswap = Integer.parseInt(swaps[0].trim());
Integer secondswap = Integer.parseInt(swaps[1].trim());
String temp = numArray[firstswap];
numArray[firstswap] = numArray[secondswap];
numArray[secondswap] = temp;
}
return numArray;
}
public static void main(String[] args) throws java.io.IOException {
File file = new File(args[0]);
BufferedReader in = new BufferedReader(new FileReader(file));
String line = "";
while ((line = in.readLine()) != null) {
if (line.length() > 0) {
String[] result = swapElements(line);
System.out.println(String.join(" ", result));
}
}
}
}