java - Java 中泛型的队列实现

标签 java generics queue

尝试使用泛型创建一个不可变队列并对其执行标准队列操作。问题是我对泛型了解不多:(请耐心等待我了解代码的流程和结构

到目前为止我已经

import java.lang.*;
import.java.util.*;
import java.util.NoSuchElementException;

public class ImmutableQueue<E>
{
  private final List<E> elmnts;

   public ImmutableQueue(List<E> elmnts) 
    {
     this.elmnts = elmnts;
     return elmnts.newInstance(); 
    }

  public ImmutableQueue() 
  {
    elmnts = new LinkedList<E>();
  }

 public ImmutableQueue<E> enqueue(E e)
 {
    if(e.equals("null"))
      throws IllegalArgumentException;

    List<E> copy = new LinkedList<E>(elmnts);
    copy.add(elmnts);
    copy.addlast(e);
    Iterator iterator = copy.iterator();
    while(iterator.hasNext())
     {
       Object prnt = itr.next();
       System.out.println(prnt+" ");
     }
   }

  public ImmutableQueue<E> dequeue()
  {
     if (elmnts.size() == 0) 
     {
      throw new NoSuchElementException();
     }

    List<E> copy = new LinkedList<E>(elmnts);
    copy.add(elmnts);
    copy.remove(0);

    Iterator iterator = copy.iterator();
     while(iterator.hasNext())
      {
       Object prnt = itr.next();
       System.out.println(prnt+" ");
      }
  } 

  public E peek()
  {
    String fsel=elmnts.get(0);
    System.out.println("First Element in the Queue is "+fsel);
    return null;
   }

  public int size()
  {
    int size=0;
    Iterator iterator = elmnts.iterator();
     while(iterator.hasNext())
     {
      size++; 
     }
    return size;
  }


     public static void main(String [] arg)
     {
        int a;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in)) ;

        ImmutableQueue<E> imqu=new LinkedList<E>();

        System.out.println("Create Queue/Add Elements to Queue ? ('N' or 'n' to stop)");
       <E> e=br.readLine();

       while(e!='n' || e!='N')
       {  
        imqu.insert(e);
       }

      System.out.println(":: Queue Operations ::");
      System.out.println("\n\n\n21.Enqueue\n2.Dequeue\n3.Peek\n4.size");

      a=Integer.parseInt(br.readLine());
      System.out.println("Choice"+a); 

    switch(a)
    {
     case 1: System.out.println("Element to be Enqueued : ");
             <E> el=br.readLine();
             imqu.enqueue(E el);
             break;

     case 2: imqu.dequeue();
             break;

     case 3: imqu.peek();
             break;

     case 4: int sz=imqu.size();
             System.out.println("Size of the queue is"+sz);
             break;

     default: System.out.println("Bad Choice");

    }
 }
}

谢谢

最佳答案

我对您发布的代码做了一些更改。这就是不可变队列的样子。不可变对象(immutable对象)不会对其进行任何更改,而是会获取该对象的副本,对其进行操作并返回相同的对象。 Java 中的 String 类就是这样的一个例子。

import java.util.LinkedList;
import java.util.List;

public class ImmutableQueue<T> {

    private List<T> immuatableQ = null;

    public ImmutableQueue() {
        this.immuatableQ = new LinkedList<T>();
    }

    public ImmutableQueue(List<T> immutableQ) {
        this();
        if (immutableQ != null) {
            this.immuatableQ.addAll(immutableQ);
        }
    }

    public ImmutableQueue<T> enqueue(T newItem) {
        List<T> copyQ = new LinkedList<T>(this.immuatableQ);
        copyQ.add(newItem);

        return new ImmutableQueue<T>(copyQ);
    }

    public ImmutableQueue<T> dequeue() {
        List<T> copyQ = new LinkedList<T>(this.immuatableQ);
        copyQ.remove(0);

        return new ImmutableQueue<T>(copyQ);
    }

    public T peek() {
        return this.immuatableQ.get(0);
    }

    public int size() {
        return this.immuatableQ.size();
    }

    public boolean isEmpty() {
        return this.immuatableQ.size() == 0;
    }

}

关于java - Java 中泛型的队列实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25501563/

相关文章:

java - 在 FileNet ContentEngine 中搜索文件夹并检索其类

java - JAX-WS 始终内联发送 MTOM 附件

generics - 在Kotlin中,如何定义具有上限的泛型类型的属性?

c# - 为什么重载决议在这里不起作用?

java - 接口(interface)类名中的标签 <>

events - 如何在 laravel 中使用 Event::queue?

Java:我可以使用Redis db创建优先级队列并根据数据集中键的值设置优先级吗

java - Jackson 自定义反序列化器在阅读列表时创建空 pojo

java - 如何将默认的水平进度条更改为垂直?

java - 实现队列,使其通用时出现错误