Iterate through Queue of Objects in Order

后端 未结 3 2069
故里飘歌
故里飘歌 2021-01-04 06:14

I have created a queue containing objects which I would like to iterate through in the order that they were placed within the queue (First object placed in queue, 2nd object

3条回答
  •  半阙折子戏
    2021-01-04 06:49

    Implement your queue as a LinkedList. Then you can iterate over your objects in order they were inserted. You have to declare the type of object being insert into the queue so you won't get any errors. You can keep it as object and specify it as a queue of objects then your code above would work. See below.

    Queue queue = new LinkedList();
    // add your objects here
    // EX: queue.add(new MyObject)
    
    for(Object item : queue){
        System.out.println(item.toString());
    }
    
        

    提交回复
    热议问题