Java获取第一个元素然后删除第一个元素打印其余元素

标签 java linked-list

这是我的代码:

import java.util.LinkedList;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.*;





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 void enqueue(E item) {
      list.addLast(item);
   }
   public E dequeue() {
      return list.poll();
   }
   public boolean hasItems() {
      return !list.isEmpty();
   }
   public boolean isEmpty()
   {
      return list == null;
   }
   public E removeFirst(){
       return list.removeFirst();
   }
   public E getFirst(){
       return list.getFirst();
   }
   public int size() {
      return list.size();
   }

   public void addItems(GenQueue<? extends E> q) {
      while (q.hasItems()) list.addLast(q.dequeue());
   }
}





public class something {

    public static void main(String[] args){

        while(true){

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

                     while (empList.hasItems()) {
                     Customer emp = empList.dequeue();
                     System.out.println(emp.firstName + " " + emp.lastName + "\n" );
                     }
//                     int choose;
//                     do{
//                       
//                     
//                     System.out.println("\nGo back to main?<1=Yes/0=No>: \n");
//                     choose = sc.nextInt();
//                     }while (choose != 1);

                     break;






                case 2:


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

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

情况 2 没有显示错误,但是当我在 IDE 上运行它时,它显示:

Exception in thread "main" java.util.NoSuchElementException at java.util.LinkedList.getFirst(Unknown Source) at GenQueue.getFirst(something.java:44) at something.main(something.java:113)

知道为什么会这样吗?以及如何解决?非常感谢您的帮助,我今天刚从 python 转到 Java,这里是新手。

最佳答案

NoSuchElementExceptionLinkedList.getFirst 抛出当列表为空时。

您应该在调用该方法之前处理该特定情况,例如:

if (!empList.hasItems()) {
    System.out.println("The queue is empty!");
} else {
    System.out.println("First in queue: "  +empList.getFirst());
    ...   // rest of code
}

关于Java获取第一个元素然后删除第一个元素打印其余元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43032818/

相关文章:

java - 如何使用 XPath 提取 XML 表数据

c - 在 C 中,为什么我的clearList函数不起作用

java - 在生产服务器中获取空通知

java - Oracle JDK 7 未安装且 E : Sub-process/usr/bin/dpkg returned an error code (1)

java - Intellij + Gradle:属性文件

java - 如何使用 WordNet (Java) 从不同单词中提取常见的上位词?

c - 用另一个值重命名列表

c - C 中没有 malloc 的链表

c - 从循环链表中释放 malloc 的内存

C——在释放内存时将字符串数据插入链表会使程序崩溃