forked from mouredev/hello-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructuresExercises.java
More file actions
79 lines (51 loc) · 2.35 KB
/
Copy pathStructuresExercises.java
File metadata and controls
79 lines (51 loc) · 2.35 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
76
77
78
79
package basic.c05_structures;
/*
Clase 44 - Ejercicios: Estructuras
Vídeo: https://youtu.be/JOAqpdM36wI?t=15680
*/
import java.util.*;
import java.util.Arrays;
import static java.lang.IO.println;
public class StructuresExercises {
public static void main(String[] args) {
// 1. Crea un Array con 5 valores e imprime su longitud.
String[] colors = {"red", "blue", "yellow", "orange", "purple", "white"};
println("Colors length: " + colors.length);
// 2. Modifica uno de los valores del Array e imprime el valor del índice antes y después de modificarlo.
println("Before modify color: " + colors[2]);
colors[2] = "black";
println("After modify color: " + colors[2]);
// 3. Crea un ArrayList vacío.
ArrayList<Integer> numbers = new ArrayList<>();
// 4. Añade 4 valores al ArrayList y elimina uno a continuación.
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.remove(2);
// 5. Crea un HashSet con 2 valores diferentes.
HashSet<String> identifiers = new HashSet<>();
identifiers.add("123");
identifiers.add("456");
// 6. Añade un nuevo valor repetido y otro sin repetir al HashSet.
identifiers.add("123");
identifiers.add("789");
// 7. Elimina uno de los elementos del HashSet.
identifiers.remove("789");
// 8. Crea un HashMap donde la clave sea un nombre y el valor el número de teléfono. Añade tres contactos.
HashMap<String, Integer> contacts = new HashMap<>();
contacts.put("Javier", 123);
contacts.put("Danni", 456);
contacts.put("Bry", 789);
// 9. Modifica uno de los contactos y elimina otro.
contacts.replace("Danni", 159);
// 10. Dado un Array, transfórmalo en un ArrayList, a continuación en un HashSet y finalmente en un HashMap con clave y valor iguales.
ArrayList<String> colorsList = new ArrayList<>(Arrays.asList(colors));
HashSet<String> colorsHashSet = new HashSet<>(Arrays.asList(colors));
HashMap<String, Integer> colorsHashMap = new HashMap<>();
for (int i = 0; i < colors.length; i++) {
colorsHashMap.putIfAbsent(colors[i], i);
println("Color ID: " + i + " || Color name: " + colors[i]);
}
}
}