-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgestionDeListas.java
More file actions
34 lines (25 loc) · 1.07 KB
/
gestionDeListas.java
File metadata and controls
34 lines (25 loc) · 1.07 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
import java.util.ArrayList;
import java.util.Scanner;
public class gestionDeListas {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> lista = new ArrayList<>();
lista.add("1");
lista.add("2");
lista.add("3");
System.out.println("Lista inicial: " + lista);
try {
System.out.print("Introduce el índice del elemento que deseas ver: ");
int indice = scanner.nextInt();
String elemento = lista.get(indice);
System.out.println("El elemento en el índice " + indice + " es: " + elemento);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: El índice está fuera del rango válido. La lista tiene " + lista.size() + " elementos.");
} catch (Exception e) {
System.out.println("Error inesperado: " + e.getMessage());
} finally {
System.out.println("Programa finalizado.");
scanner.close();
}
}
}