Saludes mis amigos en esta oportunidad veremos un ejemplo de Arraylist donde se le pide una frase ,seguidamente se le pedirá si quiere poner otra frase, así la modificara veamos
/**
*
* @author Israel Campos y Dannis Alfaro
*/
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class PracticaTDAArrayList {
public static void main(String[] args) {
//P1 HACER UN OBJETO O INSTANCIA DE ARRAYLIST
ArrayList <String> cadenas;
cadenas = new ArrayList<String>();
//P2 DEFINIR 2 VARIABLES MAS
String frase, respuesta;
//P3 LLENAR EL ARRAY LIST DINAMICAMENTE
do{
//Primero capturar la frase
frase = JOptionPane.showInputDialog(null,"Ingrese la frase");
//Segundo agregar la frase al array list
cadenas.add(frase);
//Tercero, preguntar si desea seguir agregando frases
respuesta = JOptionPane.showInputDialog(null,"¿Desea ingresar otra frase (SI/NO)?");
respuesta = respuesta.toUpperCase();
} while(respuesta.equals("SI"));
//P4 MOSTRAR EL CONTENIDO DEL ARRAYLIST (cadenas)
//Utilizaremos el metodo size y el metodo get de los arraylist
System.out.println("Las frases originales son: ");
int i;
for(i=0; i<cadenas.size(); i++){
System.out.println("<"+cadenas.get(i)+">");
}
//P5. UTILIZAR SET DE ARRARYLIST(cadenas)
System.out.println("Las frases modificadas son: ");
cadenas.set(1,"Elemento modificado") ;
for(i=0; i<cadenas.size(); i++){
System.out.println("<"+cadenas.get(i)+">");
}
//P6 UTILIZAR EL METODO REMOVE DEL ARRAYLIST(cadenas)
System.out.println("\n LAS FRASES QUE NOS QUEDAN SON: ");
cadenas.remove(0);
for(i=0; i<cadenas.size(); i++){
System.out.println("<"+cadenas.get(i)+">");
}
}
}
Continuaremos en la siguiente entrada
No hay comentarios.:
Publicar un comentario