-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwap.java
More file actions
34 lines (26 loc) · 753 Bytes
/
Copy pathSwap.java
File metadata and controls
34 lines (26 loc) · 753 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
30
31
32
33
34
package arraylist;
import java.util.ArrayList;
public class Swap {
public static void swap(ArrayList<Integer> list, int index1, int index2) {
int temp = list.get(index1);
list.set(index1, list.get(index2));
list.set(index2, temp);
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(2);
list.add(5);
list.add(9);
list.add(3);
list.add(6);
int index1 = 1;
int index2 = 3;
System.out.println("Org list: " + list);
swap(list, index1, index2);
System.out.println("Swapped list: " + list);
}
}
// TC: O(1)
// Swap two numbers
// list = 2, 5, 9, 3, 6
// index 1 = 1, index 2 = 3