Arraylist de Objetos en Java
Arraylist de Objetos en Java
Arraylist de Objetos en Java
OBJETOS EN JAVA
El siguiente es un ejemplo sencillo sobre los ArrayList, podrn encontrar los mtodos
necesarios para ingresar, modificar, buscar, eliminar y mostrar los datos del ArrayList
de Objetos. Primero encontraran la clase del objeto a usar con el ArrayList.
[showhide type="claseObjeto" more_text="+ Ver Clase Objeto ArrayList..." less_text="-
Ocultar Clase Objeto ArrayList..."]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ArryL {
private int id;
private String nom;
public ArryL(int cant, String nom){
this.id = cant;
this.nom = nom;
}
public int getId() {
return id;
}
public String getNom() {
return nom;
}
public void setNom(String d) {
this.nom = d;
}
}
[/showhide]
[showhide type="clasePrincipal" more_text="+ Ver Clase Main ArrayList..." less_text="-
Ocultar Clase Main ArrayList..."]
1
2
3
4
5
6
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class EjeArrayL {
boolean tal=false;
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
ArrayList <ArryL> Arr=null;
public static void main(String args[]){
EjeArrayL obEjeA = new EjeArrayL();
obEjeA.menu();
}
public void menu(){
String op;
do{
op = JOptionPane.showInputDialog(null,"1. Crear
ArrayList\n"
+"2. Introducir
dato\n"
+"3. Modificar dato\n"
+"4. Eliminar dato\n"
+"5. Buscar dato\n"
+"6. Mostrar datos\n"
+"7. Salir");
switch(op){
case "1"://Bloque para crear el array list
if(!tal){
Arr = new ArrayList <ArryL> ();
tal=true;
JOptionPane.showMessageDialog(null, "Ya se ha
creado el array list :D");
}else{
JOptionPane.showMessageDialog(null, "Ya esta
creado el Array List", "", JOptionPane.ERROR_MESSAGE);
}
break;
case "2"://Bloque para introducir datos al array list
if(!tal){
JOptionPane.showMessageDialog(null, "Crea el
array list, usa la opcion 1.");
}else{
int a; String b;
a =
Integer.parseInt(JOptionPane.showInputDialog(null, "Digite el numero
a ingresar: "));
b = JOptionPane.showInputDialog(null, "Digite
el nombre a ingresar: ");
Arr.add(new ArryL(a, b));
}
break;
case "3"://Bloque para modificar datos al array list
if(!tal){
JOptionPane.showMessageDialog(null, "Crea el
array list, usa la opcion 1.");
}else{
int indice, b;
String c;
b =
Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el indice
a Modificar: "));
if(existEnArray(b)){
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
indice = indiceDato(b);
c = JOptionPane.showInputDialog(null,
"Ingrese el nuevo nombre al indice: ");
Arr.get(indice).setNom(c);
}else{
JOptionPane.showMessageDialog(null, "No
existe el dato a modificar !", "", JOptionPane.ERROR_MESSAGE);
}
}
break;
case "4"://Bloque para eliminar dato del array
if(!tal){
JOptionPane.showMessageDialog(null, "No existen
datos !");
}else{
int id;
id =
Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el Id a
eliminar: "));
for(int i=0; i<Arr.size(); i++){
if(Arr.get(i).getId()==id){
Arr.remove(i);
}
}
}
break;
case "5"://Bloque para buscar datos en el array list
if(tal){
String mostrarBus= "No se ha encontrado
nada!";
int id;
id =
Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el Id a
buscar: "));
for( int i = 0 ; i < Arr.size(); i++){
if(Arr.get(i).getId()==id){
mostrarBus ="";
mostrarBus += "Numero posicion "+i+"
: "+Arr.get(i).getId();
mostrarBus += "\nNombre posicion
"+i+" : "+Arr.get(i).getNom();
mostrarBus += "\n"+"\n";
}
}
JOptionPane.showMessageDialog(null,
mostrarBus);
}else{
JOptionPane.showMessageDialog(null, "Crea el
array list, usa la opcion 1.");
}
break;
case "6"://Bloque para mostar los datos del array list
if(tal){
String Salida= "";
for( int i = 0 ; i < Arr.size(); i++){
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Salida += "Numero posicion "+i+" :
"+Arr.get(i).getId();
Salida += "\nNombre posicion "+i+" :
"+Arr.get(i).getNom();
Salida += "\n"+"\n";
}
JOptionPane.showMessageDialog(null, Salida);
}else{
JOptionPane.showMessageDialog(null, "Crea el
array list, usa la opcion 1.");
}
break;
case "7":
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Opcion
invalida !");
break;
}
}while(!op.equals("7"));
}
public boolean existEnArray(int bus){
boolean saber=false;
for(int i=0; i<= Arr.size(); i++){
if(Arr.get(i).getId()==bus){
saber=true;
break;
}
}
return saber;
}
public int indiceDato(int bus){
int j=0;
for (int i=0 ;i < Arr.size(); i++) {
if(Arr.get(i).getId()== bus){
j=i;
break;
}
}
return j;
}
}