2017-03-27 67 views
0

我有我的迭代器和deque方法的問題,在這裏實現我的代碼:的Java hasNext()和元素()在While循環

import java.util.*; 
import java.util.Iterator; 





class Customer { 
     public String lastName; 
     public String firstName; 
     public Customer() { 
     } 
     public Customer(String last, String first) { 
      this.lastName = last; 
      this.firstName = first; 
     } 
     public String toString() { 
      return firstName + " " + lastName; 
     } 
    } 
    class HourlyCustomer extends Customer { 
     public double hourlyRate; 
     public HourlyCustomer(String last, String first) { 
      super(last, first); 
     } 
    } 

class GenQueue<E> { 
    private LinkedList<E> list = new LinkedList<E>(); 
    public ListIterator<E> iterator = list.listIterator(); 
    public void enqueue(E item) { 
     list.addLast(item); 
    } 
    public E dequeue() { 
     return list.poll(); 
    } 
    public E show(){ 
     return list.peek(); 
    } 
    public void printQueueElements(){ 

    } 
    public E isNotEnd(){ 
     return list.getLast(); 
    } 
    public boolean hasItems() { 
     return !list.isEmpty(); 
    } 
    public boolean isEmpty() 
    { 
     return list.isEmpty(); 
    } 
    public Iterator<E> iterator() 
    { 
    return iterator;  
    } 


    public E removeFirst(){ 
     return list.removeFirst(); 
    } 
    public E getFirst(){ 
     return list.getFirst(); 
    } 
    public int size() { 
     return list.size(); 
    } 
    public boolean hasNext() 
    { 
    return false; 

    } 



    public void addItems(GenQueue<? extends E> q) { 

     while (q.hasNext()) list.addLast(q.dequeue()); 
    } 


} 





public class Jerald { 

    public static void main(String[] args){ 



     Scanner sc = new Scanner(System.in); 
     String input1; 
     String input2; 
     int choice = 1000; 




     GenQueue<Customer> empList; 
     empList = new GenQueue<Customer>(); 
     GenQueue<HourlyCustomer> hList; 
     hList = new GenQueue<HourlyCustomer>(); 


     while(true){ 

     do{ 

      System.out.println("================"); 
      System.out.println("Queue Operations Menu"); 
      System.out.println("================"); 
      System.out.println("1,Enquene"); 
      System.out.println("2,Dequeue"); 
      System.out.println("0, Quit\n"); 
      System.out.println("Enter Choice:"); 
      try{ 

       choice = sc.nextInt(); 



       switch(choice){ 
       case 1: 

        do{ 


        System.out.println("\nPlease enter last name: "); 
        input1 = sc.next(); 
        System.out.println("\nPlease enter first name: "); 
        input2 = sc.next(); 
        hList.enqueue(new HourlyCustomer(input1, input2)); 
        empList.addItems(hList); 

        System.out.println("\n"+(input2 + " " + input1) + " is successful queued"); 

        System.out.println("\nDo you still want to enqueue?<1> or do you want to view all in queue?<0> or \nBack to main menu for dequeueing?<menu>: "); 
        choice = sc.nextInt(); 

        }while (choice != 0); 



        System.out.println("\nThe customers' names are: \n"); 

        int numberOfElements = empList.size(); 
        for (int i = 0; i < numberOfElements; i++) { 
         Customer emp = empList.dequeue(); 
         System.out.println(emp.firstName + " " + emp.lastName + "\n"); 
         empList.enqueue(emp); 
        } 

        break; 






       case 2: 


        if (empList.isEmpty()) { 
         System.out.println("The queue is empty!"); 
        } 
        else 
        { 
        System.out.println("\nDequeued customer: " +empList.getFirst()); 
        empList.removeFirst(); 
        } 

        if (empList.isEmpty()) { 
         System.out.println("The queue is empty!"); 
        } 
        else 
        { 
        System.out.println("\nNext customer in queue: " +empList.getFirst()+"\n"); 
       } 

        break; 

       case 0: 

        System.exit(0); 



       default: 
         System.out.println("Invalid choice"); 
       } 

      } 

      catch(InputMismatchException e){ 
       System.out.println("Please enter 1-5, 0 to quit"); 
       sc.nextLine(); 
      } 

     }while(choice != 0); 
     } 
    } 
} 

的情況下,1,我試圖讓它檢索我隊列中的所有元素將其打印出來。沒有從那裏刪除它們。所以基本上,而不是使用while(hasItems)poll()我完成,它顯示我想要的輸出,但它刪除列表中的所有內容,所以我想出了另一種方法,並使用了hasNext方法,所以我用while(empLst.hasNext())element()方法,只有檢索但不刪除。不幸的是,我在這方面失敗了,並且在輸入多次之後得到一個空的結果,或者在給出一次輸入之後得到一個無限循環。我該如何解決?需要幫忙。我認爲它對我的實施,但我認爲我已經檢查過。無論如何,我需要你的意見。

順便在案例2中,im刪除鏈表的第一個元素並顯示被刪除鏈表的第一個元素。

回答

0

在情況1中,在do-while循環之後,您希望打印Queue中的所有元素並保留所有元素。

我看到2種可能性:

1)在實施GenQueue迭代訪問內部列表和打印每個元件而不改變任何一個方法:public void printQueueElements()。 這是推薦的解決方案。

2)替代:

while (empList.hasNext()) { 
    Customer emp = empList.dequeue(); 
    System.out.println(emp.firstName + " " + emp.lastName + "\n"); 
} 

用途:

int numberOfElements = empList.size(); 
for (int i = 0; i < numberOfElements; i++) { 
    Customer emp = empList.dequeue(); 
    System.out.println(emp.firstName + " " + emp.lastName + "\n"); 
    empList.enqueue(emp); 
} 

這樣隊列恢復它的元素。

對於殼體3的代碼應該是:

case 3: 
    System.out.println("\nThe customers' names are: \n"); 
    Iterator<Customer> it = empList.iterator(); 

注變量 「它」 的類型。另外,約定是調用一個迭代器變量「itr」或「iterator」。

+0

感謝的,我實現了第2步,但在第1步中如何實現它的方法是什麼?我會返回什麼? 'public void printQueueElements(){ return ??? }' – Rekt

+0

不要返回任何東西。在其名稱前帶有「void」的方法不會返回任何值。此外,看起來你並不需要任何回報,只是爲了打印到屏幕上。調用「list.listIterator(0);」獲取元素的迭代器。 –

+0

你好@Java你隔壁的人你測試了代碼在你身邊?我已經實現了它,但是當我完成添加名稱時,它仍然顯示爲空。 – Rekt

0

要消除無限循環問題,請刪除while(true)循環的外部。導致無限循環正是這些類型的循環所做的,並且你不需要它來獲得你想要的那種流。

我還介紹了菜單的第三個選項,它清除了一些東西,但我想這取決於你。

就您的列表問題而言,修復GenQueue中的「出列」方法,使其實際執行出列操作(刪除列表中的第一個元素),然後您的案例2變得容易。

要打印列表而不刪除其元素,請使用迭代器。我擺脫了你的隊列類中的ListIterator字段,因爲你不需要它。實際上,您在該課程中的大多數方法已經爲您執行了。我們來看一看 List API

class GenQueue<E> { 

    private LinkedList<E> list = new LinkedList<E>(); 

    public void enqueue(E item) { 
     list.addLast(item); 
    } 

    public E dequeue() { 

     // return a customer with null values if empty? (up to you) 
     if (list.isEmpty()) 
      return new Customer("",""); 
     else 
      return list.remove(0); 
    } 

    public E isNotEnd(){ 

     return list.getLast(); 
    } 

    public boolean hasItems() { 

     return !list.isEmpty(); 
    } 

    public boolean isEmpty() { 

     return list.isEmpty(); 
    } 

    public Iterator<E> iterator() { 

     return list.iterator(); 
    } 

    public E removeFirst() { 

     return list.removeFirst(); 
    } 

    public E getFirst() { 

     return list.getFirst(); 
    } 

    public int size() { 

     return list.size(); 
    } 

    public boolean hasNext() { 

     return false; 
    } 

    public void addItems(GenQueue<? extends E> q) { 

     while (q.hasNext()) list.addLast(q.dequeue()); 
    } 
} 


public class something { 

    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 
     String input1; 
     String input2; 
     int choice = 1000; 

     GenQueue<Customer> empList; 
     empList = new GenQueue<Customer>(); 
     GenQueue<HourlyCustomer> hList; 
     hList = new GenQueue<HourlyCustomer>(); 

     do { 

      System.out.println("================"); 
      System.out.println("Queue Operations Menu"); 
      System.out.println("================"); 
      System.out.println("1,Enquene"); 
      System.out.println("2,Dequeue"); 
      System.out.println("3,View queue"); 
      System.out.println("0, Quit\n"); 
      System.out.println("Enter Choice:"); 

      try { 

       choice = sc.nextInt(); 

       switch(choice) { 

        case 1: 

         System.out.println("\nPlease enter last name: "); 
         input1 = sc.next(); 
         System.out.println("\nPlease enter first name: "); 
         input2 = sc.next(); 
         hList.enqueue(new HourlyCustomer(input1, input2)); 
         empList.addItems(hList); 

         System.out.println("\n"+(input2 + " " + input1) + " is successful queued"); 

         break; 

        case 2: 

         System.out.println("Dequeued customer: " + empList.dequeue().toString()); 

         break; 

        case 3: 

         System.out.println("\nThe customers' names are: \n"); 

         Iterator<Genqueue<Customer>> it = empList.iterator(); 

         while (it.hasNext()) { 

          Customer emp = it.next(); 
          System.out.println(emp.firstName + " " + emp.lastName + "\n"); 
         } 

         break; 

        case 0: 

         System.exit(0); 

        default: 

         System.out.println("Invalid choice"); 
       } 
      } 
      catch(InputMismatchException e) { 

       System.out.println("Please enter 1-5, 0 to quit"); 
       sc.nextLine(); 
      } 

     } while(choice != 0); 
    } 
} 
+0

我得到這個誤差的情況下,3: 線154和158 154: '類型不匹配:不能從迭代轉換爲迭代>' 158: '類型不匹配:不能從GenQueue轉換轉發給客戶' – Rekt

+0

在公衆E dequeue '類型不匹配:不能從客戶轉換爲E' @ ClumsySyrup – Rekt

+0

你發現問題了嗎?由於錯誤,我不能運行它,即使我嘗試通過建議更改自己的代碼,但仍然在添加到隊列後返回沒有列表元素。 – Rekt