2017-03-27 68 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