Skip to content

Commit 5ccdddf

Browse files
committed
3.70 & 3.71
1 parent 401dc54 commit 5ccdddf

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//Create the method public static void combine(ArrayList<Integer> first, ArrayList<Integer> second)
2+
// that inserts the items in a list called second to a list called first.
13

24
import java.util.ArrayList;
35
import java.util.Collections;
@@ -6,28 +8,21 @@ public class CombiningArrayLists {
68

79
public static void main(String[] args) {
810

9-
// You can change the values while testing
1011
ArrayList<Integer> list1 = new ArrayList<Integer>();
1112
ArrayList<Integer> list2 = new ArrayList<Integer>();
1213

1314
Collections.addAll(list1, 4, 3);
14-
// A shorthand for:
15-
// list1.add(4);
16-
// list1.add(3);
17-
18-
1915
Collections.addAll(list2, 5, 10, 7);
20-
// A shorthand for:
21-
// list2.add(5);
22-
// list2.add(10);
23-
// list2.add(7);
2416

25-
// Implement the following method and then remove the comment
26-
// combine(list1, list2);
17+
combine(list1, list2);
2718
System.out.println(list1);
2819
System.out.println(list2);
20+
}
2921

30-
22+
public static void combine(
23+
ArrayList<Integer> first,
24+
ArrayList<Integer> second) {
25+
first.addAll(second);
3126
}
3227

3328
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//Create the method smartCombine that works like the previous combine method
2+
// except that numbers can be on the list only once.
3+
14
import java.util.ArrayList;
25
import java.util.Collections;
36

@@ -8,13 +11,20 @@ public static void main(String[] args) {
811
ArrayList<Integer> list2 = new ArrayList<Integer>();
912

1013
Collections.addAll(list1, 4, 3);
11-
1214
Collections.addAll(list2, 5, 10, 4, 3, 7);
1315

14-
// remove comment when method ready
15-
//smartCombine(list1, list2);
16+
smartCombine(list1, list2);
1617
System.out.println(list1);
1718
System.out.println(list2);
1819
}
1920

21+
public static void smartCombine(
22+
ArrayList<Integer> first,
23+
ArrayList<Integer> second) {
24+
25+
for (int num : second) {
26+
if (first.contains(num)) continue;
27+
first.add(num);
28+
}
29+
}
2030
}

0 commit comments

Comments
 (0)