-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavalab22.java
More file actions
30 lines (28 loc) · 925 Bytes
/
Copy pathjavalab22.java
File metadata and controls
30 lines (28 loc) · 925 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
import java.util.*;
public class javalab22 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList <String> List = new ArrayList<>();
System.out.print("Enter number of elements in the string type List: ");
int n = sc.nextInt();
String st;
sc.nextLine();
for(int i = 0;i < n; i++){
System.out.println(" Enter element " + (i+1) + ":");
st = sc.nextLine();
List.add(st);
}
Swap(List);
System.out.println("List after swapping of elelments: ");
System.out.println(List);
}
static void Swap(ArrayList<String> list){
String st;
for(int i = 0;i < list.size()-1; i+=2){
// st = list.get(i);
// list.set(i, list.get(i + 1));
// list.set(i + 1, st);
Collections.swap(list, i, i+1);
}
}
}