KaiquanMah commited on
Commit
66a531a
·
verified ·
1 Parent(s): 6e23d95

ArrayList<String> list1 = new ArrayList<String>(List.of("item1", ...));

Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/14B. List of Seven Brothers ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Initialize a string type list, with values being the 7 brothers named "Edward", "William","Henry","George","Charles","Thomas" and "James".
2
+
3
+ The list should be of 'ArrayList' type.
4
+ The reference is stored in a variable named brothers.
5
+
6
+
7
+
8
+ import java.util.Random;
9
+ import java.util.ArrayList;
10
+ import java.util.Collections;
11
+ import java.util.Arrays;
12
+ import java.util.List;
13
+
14
+ public class Test{
15
+ public static void main(String[] args){
16
+ final Random r = new Random();
17
+
18
+ //ADD
19
+ ArrayList<String> brothers = new ArrayList<String>(List.of("Edward", "William","Henry","George","Charles","Thomas" ,"James"));
20
+
21
+
22
+
23
+
24
+ System.out.println("brothers is an ArrayList: " + (brothers.getClass() == ArrayList.class));
25
+
26
+ Collections.sort(brothers);
27
+
28
+ System.out.println("Brothers:");
29
+ brothers.stream().forEach(b -> System.out.println(b));
30
+ }
31
+ }
32
+
33
+
34
+
35
+
36
+ brothers is an ArrayList: true
37
+ Brothers:
38
+ Charles
39
+ Edward
40
+ George
41
+ Henry
42
+ James
43
+ Thomas
44
+ William
45
+
46
+
47
+