-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswap.java
More file actions
26 lines (23 loc) · 766 Bytes
/
swap.java
File metadata and controls
26 lines (23 loc) · 766 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class swap {
public static void Swap(List<Integer> list, int idx1, int idx2) {
int temp = list.get(idx1);
list.set(idx1, list.get(idx2));
list.set(idx2, temp);
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(Arrays.asList(2, 5, 9, 3, 6));
int idx1 = 1, idx2 = 4;
System.out.println("Before Swap");
System.out.println(list);
Swap(list, idx1, idx2);
System.out.println("After Swap");
System.out.println(list);
Collections.sort(list);
System.out.println("After Sorting");
System.out.println(list);
}
}