我有我的迭代器和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删除链表的第一个元素并显示被删除链表的第一个元素。
感谢的,我实现了第2步,但在第1步中如何实现它的方法是什么?我会返回什么? 'public void printQueueElements(){ return ??? }' – Rekt
不要返回任何东西。在其名称前带有“void”的方法不会返回任何值。此外,看起来你并不需要任何回报,只是为了打印到屏幕上。调用“list.listIterator(0);”获取元素的迭代器。 –
你好@Java你隔壁的人你测试了代码在你身边?我已经实现了它,但是当我完成添加名称时,它仍然显示为空。 – Rekt