美文网首页
LinkedList模拟队列

LinkedList模拟队列

作者: 一花一世界yu | 来源:发表于2020-04-20 20:12 被阅读0次
    /*
        使用LinkedList,模拟队列,模拟流水线生产矿泉水添加瓶盖.
        封装起来,真正的队列模式
     */
    
    import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Objects;
    
    public class HomeWork {
        public static void main(String[] args) {
            //创建瓶子对象
            Bottle bottle1 = new Bottle("农夫山泉", 1);
            Bottle bottle2 = new Bottle("农夫山泉", 2);
            Bottle bottle3 = new Bottle("农夫山泉", 3);
            //模拟队列得存储过程
            //入
           Processed.putInQueue(bottle1);
           Processed.putInQueue(bottle2);
           Processed.putInQueue(bottle3);
            //出
            Processed.outQueue();
            Processed.outQueue();
            Processed.outQueue();
        }
    }
    class Bottle{
        private String name;
        private int id;
    
        public Bottle(String name, int id) {
            this.name = name;
            this.id = id;
        }
    
        @Override
        public String toString() {
            return "bottle{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    '}';
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Bottle bottle = (Bottle) o;
            return id == bottle.id &&
                    Objects.equals(name, bottle.name);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    }
    class Processed{
       private static LinkedList<Bottle> bottles = new LinkedList<>();
        public static void putInQueue(Bottle bottle1){
            bottles.offer(bottle1);
        }
        public static void outQueue(){
            System.out.println(bottles.poll());
        }
    }
    

    相关文章

      网友评论

          本文标题:LinkedList模拟队列

          本文链接:https://www.haomeiwen.com/subject/dbnjihtx.html