-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapPairs.java
More file actions
71 lines (57 loc) · 2.34 KB
/
SwapPairs.java
File metadata and controls
71 lines (57 loc) · 2.34 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class SwapPairs {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Swap Pairs in ArrayList");
System.out.println("----------------------");
// Create an ArrayList of strings
ArrayList<String> strings = new ArrayList<>();
// Get input from user
System.out.println("Enter strings (enter 'done' to finish):");
while (true) {
System.out.print("> ");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("done")) {
break;
}
strings.add(input);
}
// Display the original list
System.out.println("\nOriginal list:");
System.out.println(strings);
// Swap pairs in the list
swapPairs(strings);
// Display the modified list
System.out.println("\nAfter swapping pairs:");
System.out.println(strings);
// Example with predefined list for demo
System.out.println("\nExample with predefined list:");
ArrayList<String> example1 = new ArrayList<>(
Arrays.asList("four", "score", "and", "seven", "years", "ago")
);
System.out.println("Original: " + example1);
swapPairs(example1);
System.out.println("After swapping: " + example1);
// Example with odd number of elements
System.out.println("\nExample with odd number of elements:");
ArrayList<String> example2 = new ArrayList<>(
Arrays.asList("to", "be", "or", "not", "to", "be", "hamlet")
);
System.out.println("Original: " + example2);
swapPairs(example2);
System.out.println("After swapping: " + example2);
scanner.close();
}
// Method to swap pairs of elements in an ArrayList
public static void swapPairs(ArrayList<String> list) {
// Iterate through the list in pairs
for (int i = 0; i < list.size() - 1; i += 2) {
// Swap elements at index i and i+1
String temp = list.get(i);
list.set(i, list.get(i + 1));
list.set(i + 1, temp);
}
}
}