Reputation: 21
So I been working on BlueJ and right now I have found a small problem don't know if the solution is easy but I'm stuck at a part that I need to maybe use 2 iterators but can´t seem to make them work.
the problem that I have is with the last part of the code
for (Iterator i = cliente.iterator(); i.hasNext(); & Iterator j = articulo.iterator(); j.hasNext())
{
System.out.println(i.next());
System.out.println(j.next());
It works fine with only 1 ArrayList and 1 iterator, when I only use the iterator i
, it gives me all the info that is on that array same if I only use the iterator j
that is without putting articulo when I use the i or without cliente when I use j. So yea thing I need to work is that it gives me all the info that is in boths arrays, don't know if there is a way to do it differently but that's why I ask.
import java.util.ArrayList;
import java.util.Iterator;
public class Encabezado
{
private ArrayList<Cliente> cliente;
private ArrayList<Articulo> articulo;
private String NFactura;
private String fecha;
public Encabezado()
{
cliente = new ArrayList<Cliente>();
articulo = new ArrayList<Articulo>();
}
public Encabezado(String NFactura, String fecha)
{
cliente = new ArrayList<Cliente>();
articulo = new ArrayList<Articulo>();
this.NFactura = NFactura;
this.fecha = fecha;
}
public void AgregarCliente(Cliente c)
{
cliente.add(c);
}
public void AgregarArticulo(Articulo a)
{
articulo.add(a);
}
public void verFactura ()
{
System.out.println("Factura: " + NFactura);
for (Iterator i = cliente.iterator(); i.hasNext(); & Iterator j = articulo.iterator(); j.hasNext())
{
System.out.println(i.next());
System.out.println(j.next());
}
}
}
Upvotes: 0
Views: 73
Reputation: 1245
if you want to use two iterator simultaneously you can do as below,
Iterator<Cliente> i = cliente.iterator();
Iterator<Articulo> j = articulo.iterator();
while (i.hasNext() && j.hasNext()) {
System.out.println(i.next());
System.out.println(j.next());
}
Upvotes: 3