简体   繁体   English

Java中泛型的队列实现

[英]Queue implementation with Generics in Java

trying to make an immutable queue and performing standard queue operations on it,using generics.the problem is i dont know much about generics :( please bear with me about the flow and structure of the code 试图使一个不可变的队列,并使用泛型对其执行标准的队列操作。问题是我对泛型不是很了解:(请耐心等待代码的流程和结构

so far i have 到目前为止,我有

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");

    }
 }
}

thanks 谢谢

I have done some change in the code that you have posted. 我对您发布的代码进行了一些更改。 This is how an immutable queue should look like. 这就是不可变队列的外观。 Immutable objects won't do any changes on it rather it will take a copy of the object, manipulates it and returns the same. 不可变的对象不会对其进行任何更改,而是会复制该对象,对其进行操作并返回相同的对象。 String class in java is one such example. Java中的字符串类就是这样的例子。

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;
    }

}

Look at this example implementation of Queue using Generis in Java. 查看Java中使用Generis的Queue的示例实现。 java queue implemenation You can read about Java Generics here Java Generics java的队列实行你可以读到这里Java泛型Java泛型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM