In what way can I go through the vector?

0

How can I do to show all the information of all the Clients using a Vector ?

import javax.swing.*;
import java.util.*;

public class AgregarClientes {

    public AgregarClientes() {
        Vector vector = new Vector();

        Clientes clientes = new Clientes();
        clientes.setNombre(JOptionPane.showInputDialog("Digite el nombre"));
        clientes.setApellido(JOptionPane.showInputDialog("Digite el apellido"));
        clientes.setIdentificacion(Double.parseDouble(JOptionPane.showInputDialog("Digite la identificacion")));
        clientes.setTelefono(Double.parseDouble(JOptionPane.showInputDialog("Digite el telefono")));
        clientes.setTipocliente(Integer.parseInt(JOptionPane.showInputDialog("Digite el tipo de cliente")));
        clientes.setTipocliente(Integer.parseInt(JOptionPane.showInputDialog("Digite el tipo de vehiculo")));
        clientes.setHoras(Integer.parseInt(JOptionPane.showInputDialog("Digite el numero de horas")));
        clientes.setPlaca(JOptionPane.showInputDialog("Digite la placa del vehiculo"));       
        vector.addElement(clientes);

    }
}
    
asked by Steven Camargo 26.10.2017 в 20:35
source

1 answer

3

First of all for the use of that collection I recommend you specify the type of data that it will contain, this is done so

Vector<Clientes> vector = new Vector<Clientes>();

and if you have java 8 you can infer the second parameter

Vector<Clientes> vector = new Vector<>();

One way is with the enhanced can be used from Java 1.7.

The improved for is as follows

for(TipoDeDatos cualquierNombreDeVariable:nombreDeColeccion){
       System.out.println(cualquierNombreDeVariable.getCampo());
}

your collection must implement the iterable interface, and this is the case of Vector.

your code would be:

import javax.swing.*;
import java.util.*;

public class AgregarClientes {

    public AgregarClientes() {
        Vector<Clientes> vector = new Vector<Clientes>();

        Clientes clientes = new Clientes();
        clientes.setNombre(JOptionPane.showInputDialog("Digite el nombre"));
        clientes.setApellido(JOptionPane.showInputDialog("Digite el apellido"));
        clientes.setIdentificacion(Double.parseDouble(JOptionPane.showInputDialog("Digite la identificacion")));
        clientes.setTelefono(Double.parseDouble(JOptionPane.showInputDialog("Digite el telefono")));
        clientes.setTipocliente(Integer.parseInt(JOptionPane.showInputDialog("Digite el tipo de cliente")));
        clientes.setTipocliente(Integer.parseInt(JOptionPane.showInputDialog("Digite el tipo de vehiculo")));
        clientes.setHoras(Integer.parseInt(JOptionPane.showInputDialog("Digite el numero de horas")));
        clientes.setPlaca(JOptionPane.showInputDialog("Digite la placa del vehiculo"));       
        vector.addElement(clientes);


     for(Clientes cliente:vector){
       System.out.println(cliente.getNombre());
     }
    }
}
    
answered by 26.10.2017 / 20:44
source