How can I traverse a queue in java?

1

I'm trying to go through a Queue list in java. I have created a method to traverse the tail that is called Walk ().  I'm using eclipse so try this: list Wait.peek (). Get (i), but I do not recognize the get (i) method.

import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;

public class ColeccionVehEspera {
    Queue<Vehiculo> listaEspera;//= new Queue<Vehiculo>();

    public ColeccionVehEspera() {

        this.listaEspera = new Queue<Vehiculo>() {

            @Override
            public boolean addAll(Collection<? extends Vehiculo> arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void clear() {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean contains(Object arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean containsAll(Collection<?> arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public Iterator<Vehiculo> iterator() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public boolean remove(Object o) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean removeAll(Collection<?> c) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean retainAll(Collection<?> c) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public int size() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public Object[] toArray() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public <T> T[] toArray(T[] a) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public boolean add(Vehiculo e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public Vehiculo element() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public boolean offer(Vehiculo e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public Vehiculo peek() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Vehiculo poll() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Vehiculo remove() {
                // TODO Auto-generated method stub
                return null;
            }
        };
    }



    public void recorrerEspera()  {

        for (int i = 0; i < listaEspera.size(); i++) {
            System.out.println(listaEspera.peek());
        }

    }

}
    
asked by David Palanco 03.06.2018 в 19:36
source

1 answer

1

You can implement it as a linked list, in order to traverse it, what you do does not work because in your case peek() returns a Vehicle which does not have the get() method, apart from that you did not define an implementation of that method for your tail.

import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
import java.util.LinkedList;

    public class ColeccionVehEspera {
    Queue<Vehiculo> listaEspera = new LinkedList<Vehiculo>();

    public ColeccionVehEspera(){
      listaEspera.add(new Vehiculo());
      listaEspera.add(new Vehiculo());
    }

    public void recorrerEspera()  {
      for(Vehiculo v: listaEspera){
        System.out.println(v.toString());
      }
    }
}
    
answered by 04.06.2018 в 03:13