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
106 lines (69 loc) · 3.09 KB
/
Copy pathStructuresExercises.java
File metadata and controls
106 lines (69 loc) · 3.09 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.*;
public class StructuresExercises {
public static void main (String[] args) {
// 1. Crea un Array con 5 valores e imprime su longitud.
String[] cities = {"Bogota","Cali","Medellin", "Pereira", "Manizales"};
System.out.println(cities.length);
System.out.println(cities[0]);
// 2. Modifica uno de los valores del Array e imprime el valor del índice antes
// y después de modificarlo.
System.out.println(cities[2]);
cities[2] = "Ibague";
System.out.println(cities[2]);
// 3. Crea un ArrayList vacío.
var animals = new ArrayList<String>();
System.out.println(animals.size());
// 4. Añade 4 valores al ArrayList y elimina uno a continuación.
animals.add("Perro");
animals.add("Gato");
animals.add("Loro");
animals.add("Hamster");
System.out.println(animals);
animals.remove(1);
System.out.println(animals);
// 5. Crea un HashSet con 2 valores diferentes.
HashSet<String> colors = new HashSet<>();
colors.add("Azul");
colors.add("Rojo");
System.out.println(colors);
// 6. Añade un nuevo valor repetido y otro sin repetir al HashSet.
colors.add("Azul");
colors.add("Verde");
System.out.println(colors);
// 7. Elimina uno de los elementos del HashSet.
colors.remove("Rojo");
System.out.println(colors);
// 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, String> addressBook = new HashMap<>();
addressBook.put("Jorge", "3014114135");
addressBook.put("Andres", "3214951871");
addressBook.put("Maria", "3005600672");
System.out.println(addressBook);
// 9. Modifica uno de los contactos y elimina otro.
System.out.println(addressBook.get("Andres"));
addressBook.replace("Andres", "3005688937");
System.out.println(addressBook.get("Andres"));
// 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.
// Declarar el array
ArrayList<String> myFruits = new ArrayList<>();
String[] fruits = {"Manzana", "Pera", "Banano", "Fresa"};
System.out.println(fruits[0]);
// Convertir el array en arrayList
System.out.println("Convertir array en arrayList");
Collections.addAll(myFruits, fruits);
System.out.println(myFruits);
// Convertir ArrayList en HashSet
System.out.println("Convertir ArrayList en HashSet");
HashSet<String> hashFruits = new HashSet<>(myFruits);
System.out.println(hashFruits);
// Convertir HashSet en HashMap
System.out.println("Convertir HashSet a HashMap");
HashMap<String, String> mapFruits = new HashMap<>();
for (String fruit : hashFruits) {
mapFruits.put(fruit, fruit);
}
System.out.println(mapFruits);
}
}