-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathManipulatingArrays.java
More file actions
75 lines (45 loc) · 2.38 KB
/
ManipulatingArrays.java
File metadata and controls
75 lines (45 loc) · 2.38 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
72
73
74
75
package ocp.chapter.five;
import java.util.*;
public class ManipulatingArrays {
static int[] classIntArray;
public static void main(String... args) {
String [] bugs = new String[] { "cricket", "beetle", "ladybug" };
System.out.println(Arrays.toString(bugs));
var emptyList = new ArrayList<>();
String[] emptyArray = emptyList.toArray(new String[0]);
System.out.println(emptyList);
System.out.println(emptyArray);
String[] oneArray = {"hawk", "robin"};
List<String> oneAsList = Arrays.asList(oneArray);
List<String> oneListOf = List.of(oneArray);
System.out.println(oneAsList);
System.out.println(oneListOf);
classIntArray = new int[] {1, 2, 3}; // Need to instantiate a new object to change the default values of the array
System.out.println(Arrays.toString(classIntArray));
System.out.println("\n----------------------------------------\n");
int numbers[] = new int[] {6, 9, 1};
Arrays.sort(numbers);
for(Integer i : numbers) // Autoboxing with the sorted int array
System.out.println(i + " "); // 1 6 9
System.out.println("\n");
String[] strings = {"123", "9", "112", "0300"};
Arrays.sort(strings);
for(String s : strings)
System.out.println(s + " "); // "0300", "112", "123", "9" - alphabetic order - sorts from 0 to 9, then from A to Z and finally from a to z.
System.out.println("\n----------------------------------------\n");
Set<Integer> set = new HashSet<>();
System.out.println("Added the value? " + set.add(21)); // true
System.out.println("Added the duplicated value? " + set.add(21)); // false
System.out.println("Size: " + set.size()); // 1
set.remove(21);
System.out.println("isEmpty? " + set.isEmpty()); // true
System.out.println("\n----------------------------------------\n");
Map<String, String> map = new HashMap<>();
map.put("koala", "bamboo");
String food = map.get("koala"); // bamboo
String other = map.getOrDefault("ant", "leaf"); // leaf
for (String key: map.keySet())
System.out.println(key + " " + map.get(key)); // koala bamboo (key | value)
System.out.println("\n----------------------------------------\n");
}
}