-
Notifications
You must be signed in to change notification settings - Fork 48
/
ch03_ItineraryCopying.java
49 lines (41 loc) · 1.8 KB
/
ch03_ItineraryCopying.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.ArrayList;
import java.util.List;
public class ch03_ItineraryCopying {
static List<String> replan(List<String> plan,
String newCity,
String beforeCity) {
int newCityIndex = plan.indexOf(beforeCity);
plan.add(newCityIndex, newCity);
return plan;
}
static List<String> replanPure(List<String> plan,
String newCity,
String beforeCity) { // named replan in the book
int newCityIndex = plan.indexOf(beforeCity);
List<String> replanned = new ArrayList<>(plan);
replanned.add(newCityIndex, newCity);
return replanned;
}
public static void main(String[] args) {
List<String> planA = new ArrayList<>();
planA.add("Paris");
planA.add("Berlin");
planA.add("Kraków");
System.out.println("Plan A: " + planA);
List<String> planB = replan(planA, "Vienna", "Kraków");
assert(planB.toString().equals("[Paris, Berlin, Vienna, Kraków]"));
System.out.println("Plan B: " + planB);
assert(planA.toString().equals("[Paris, Berlin, Vienna, Kraków]"));
System.out.println("Plan A: " + planA);
List<String> planAPure = new ArrayList<>();
planAPure.add("Paris");
planAPure.add("Berlin");
planAPure.add("Kraków");
System.out.println("Plan A (pure): " + planAPure);
List<String> planBPure = replanPure(planAPure, "Vienna", "Kraków");
assert(planBPure.toString().equals("[Paris, Berlin, Vienna, Kraków]"));
System.out.println("Plan B (pure): " + planBPure);
assert(planAPure.toString().equals("[Paris, Berlin, Kraków]"));
System.out.println("Plan A (pure): " + planAPure);
}
}