1
+ // 03 - Java
2
+ // Estructuras de datos
3
+
4
+ import java .util .*;
5
+
6
+ public class inmortalnight {
7
+ public static void main (String [] args ) {
8
+ //Creación de estructuras de datos
9
+ //NOTA: en Java, al declarar ya se establece un tipo de dato para toda la estructura.
10
+ //Array, estructura de tamaño fijo
11
+ int [] array = {1 , 2 , 3 , 4 , 5 }; //Operación de insercción
12
+ array [0 ] = 1 ; //Operación de actualización
13
+ Arrays .sort (array ); //Operación de ordenación
14
+
15
+ //Lista (ArrayList), estructura de tamaño variable de cualquier tipo
16
+ ArrayList <Integer > lista = new ArrayList <Integer >();
17
+ lista .add (1 ); //Operación de insercción
18
+ lista .add (2 );
19
+ lista .remove (1 ); //Operación de borrado
20
+ lista .set (0 , 10 ); //Operación de actualización
21
+ lista .sort (null ); //Operación de ordenación
22
+
23
+ //Set, estructura de valores únicos
24
+ Set <Integer > set = new HashSet <Integer >();
25
+ set .add (1 ); //Operación de insercción
26
+ set .remove (1 ); //Operación de borrado
27
+
28
+ //Map, estructura de clave-valor, donde la clave es única
29
+ Map <String , Integer > map = new HashMap <String , Integer >();
30
+ map .put ("uno" , 1 ); //Operación de insercción
31
+ map .remove ("uno" ); //Operación de borrado
32
+ map .put ("uno" , 10 ); //Operación de actualización
33
+
34
+ //Queue, estructura de datos que sigue el orden FIFO
35
+ Queue <Integer > queue = new LinkedList <Integer >();
36
+ queue .add (1 ); //Operación de insercción
37
+ queue .remove (); //Operación de borrado
38
+
39
+ //Stack, estructura de datos que sigue el orden LIFO
40
+ Stack <Integer > stack = new Stack <Integer >();
41
+ stack .push (1 ); //Operación de insercción
42
+ stack .pop (); //Operación de borrado
43
+
44
+ //EXTRA: Crear una agenda de contactos
45
+ EjercicioExtra ();
46
+ }
47
+
48
+ public static void EjercicioExtra () {
49
+ Scanner sc = new Scanner (System .in );
50
+ Map <String , Integer > agenda = new HashMap <>();
51
+ int opcion = 0 ;
52
+ do {
53
+ System .out .println ("\n *** Agenda ***" );
54
+ System .out .println ("Seleccione una opción:" );
55
+ System .out .println ("1. Buscar Contacto" );
56
+ System .out .println ("2. Nuevo Contacto" );
57
+ System .out .println ("3. Actualizar Contacto" );
58
+ System .out .println ("4. Eliminar Contacto" );
59
+ System .out .println ("5. Salir" );
60
+ opcion = sc .nextInt ();
61
+ switch (opcion ) {
62
+ case 1 :
63
+ BuscarContacto (sc , agenda );
64
+ break ;
65
+ case 2 :
66
+ NuevoContacto (sc , agenda );
67
+ break ;
68
+ case 3 :
69
+ ActualizarContacto (sc , agenda );
70
+ break ;
71
+ case 4 :
72
+ EliminarContacto (sc , agenda );
73
+ break ;
74
+ case 5 :
75
+ System .out .println ("Saliendo..." );
76
+ break ;
77
+ default :
78
+ System .out .println ("Opción no válida" );
79
+ break ;
80
+ }
81
+ } while (opcion != 5 );
82
+ sc .close ();
83
+ }
84
+
85
+ public static void BuscarContacto (Scanner sc , Map <String , Integer > agenda ) {
86
+ System .out .print ("Ingrese el nombre del contacto: " );
87
+ String nombre = sc .next ();
88
+ if (agenda .containsKey (nombre )) {
89
+ System .out .println ("Teléfono de " + nombre + ": " + agenda .get (nombre ));
90
+ } else {
91
+ System .out .println ("Contacto no encontrado." );
92
+ }
93
+ }
94
+
95
+ public static void NuevoContacto (Scanner sc , Map <String , Integer > agenda ) {
96
+ System .out .print ("Ingrese el nombre del nuevo contacto: " );
97
+ String nombre = sc .next ();
98
+ System .out .print ("Ingrese el número de teléfono: " );
99
+ String telefono = sc .next ();
100
+ if (telefono .length () <= 11 && telefono .matches ("\\ d+" )) {
101
+ agenda .put (nombre , Integer .parseInt (telefono ));
102
+ System .out .println ("Contacto agregado." );
103
+ } else {
104
+ System .out .println ("Número de teléfono inválido. Debe ser numérico y no más de 11 dígitos." );
105
+ }
106
+ }
107
+
108
+ public static void ActualizarContacto (Scanner sc , Map <String , Integer > agenda ) {
109
+ System .out .print ("Ingrese el nombre del contacto a actualizar: " );
110
+ String nombre = sc .next ();
111
+ if (agenda .containsKey (nombre )) {
112
+ System .out .print ("Ingrese el nuevo número de teléfono: " );
113
+ String telefono = sc .next ();
114
+ if (telefono .length () <= 11 && telefono .matches ("\\ d+" )) {
115
+ agenda .put (nombre , Integer .parseInt (telefono ));
116
+ System .out .println ("Contacto actualizado." );
117
+ } else {
118
+ System .out .println ("Número de teléfono inválido. Debe ser numérico y no más de 11 dígitos." );
119
+ }
120
+ } else {
121
+ System .out .println ("Contacto no encontrado." );
122
+ }
123
+ }
124
+
125
+ public static void EliminarContacto (Scanner sc , Map <String , Integer > agenda ) {
126
+ System .out .print ("Ingrese el nombre del contacto a eliminar: " );
127
+ String nombre = sc .next ();
128
+ if (agenda .containsKey (nombre )) {
129
+ agenda .remove (nombre );
130
+ System .out .println ("Contacto eliminado." );
131
+ } else {
132
+ System .out .println ("Contacto no encontrado." );
133
+ }
134
+ }
135
+ }
0 commit comments