我有我的迭代器和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