viernes, 30 de julio de 2010

EJEMPLO DE ARRAYLIST

Creamos una clase con sus atributos privados
public class Producto{
private String nombre;
private int cantidad;
Cada uno con sus get and set
      private void setnombre (String nom){
            nombre=nom;
      }
     private void setnombre (int cant){
            cantidad=cant;

      }
     private String getnombre(){
            return nombre;
     }
      private String getnombre(){
            return nombre;
                                                                           }
Creamos los constructores el vacio y el sobrecargado
     public Producto{
     }
      public Producto(String nom,int cant){
              nombre=nom;
              canidad=cant;
     }
  }

En el Main hacemos lo siguiente

public class Main{                                          
     public void class main(String[] args){
Creamos 2 instancias de la clase Producto         
     Producto a = new Producto ("disco duro", 33256);
     Producto b = new Producto ("cable sata", 11656);
Creamos un ArrayList
      ArrayList lista = new ArrayList();
Insertamos los objetos
      lista.add(a);
      lista.add(b);
Creamos un metodo para mostrar
     mostrar(lista);


     }
Metodo para mostrar el ArrayList
      private void mostrar(List lista){
           for(iterator it = lista.iterator().hasNext()){
           Producto x = (Producto)it.Next();
           sout(x.getName+":"+x.getcantidad);
          }
      }

1 comentario:

  1. The Best Casino Sites in Nigeria | Lucky Club
    With a great reputation in the gaming industry, you have to be sure to enjoy the best gaming luckyclub experiences. You can trust our experts for What are the best slots sites in Nigeria?Are there free slots in Nigeria?

    ResponderEliminar

/*===============medicion============================*/