为什么我的迭代器 foreach 循环从不 enter/execute?
Why does my iterator foreach loop never enter/execute?
我在方法 toString() 中有一个 for-each 循环,它应该遍历通用 FIFO 队列中的元素(使用双向链表数据结构实现)并将队列中的项目连接到字符串 a 上,这返回到打印字符串的 enqueue(),它表示我的队列及其在 enqueue 调用后的内容。我的问题是,为什么 for-each 根本不是 executed/entered?
我尝试插入 System.out.print("Hi");在 for-each 里面,它没有打印出来。所以我假设某些代码块阻碍了它正常执行。
// FIFOQueue is implemented using the structure double linked list (DLL)
// Generic, iterable
import java.util.Iterator;
import java.util.*;
public class FIFOQueueDLL<Item> implements Iterable<Item>{
private Node first;
private Node last;
private int length = 0;
// is the queue empty?
public boolean isEmpty(){
return length == 0;
}
private class Node{
Item item;
Node next;
Node previous;
}
// add an item
public void enqueue(Item n){
Node newnode = new Node();
newnode.item = n;
if(isEmpty()){
last = newnode;
} else {
first.previous = newnode;
}
newnode.next = first;
first = newnode;
length++;
System.out.println(this);
}
// remove and return the least recently added item
public Item dequeue(){
if(isEmpty()){
throw new NoSuchElementException();
}
Node t = last;
if(first == last){
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
t.previous = null;
length--;
System.out.println(this.toString(););
return t.item;
}
public String toString(){
String a = "123";
for(Item item : this){
a = a + item;
}
return a;
}
public Iterator<Item> iterator(){
return new FIFOIterator();
}
private class FIFOIterator implements Iterator<Item>{
// Declare attribute
Node curr;
// Set attribute of node curr
public FIFOIterator(){
Node curr = first;
curr.item = first.item;
curr.next = first.next;
}
//private int i = length;
public boolean hasNext(){
return curr != null;
}
public Item next(){
Item a = curr.item;
curr = curr.next;
return a;
}
}
public static void main(String[] args){
FIFOQueueDLL<Character> c = new FIFOQueueDLL<Character>();
char b = 'b';
c.enqueue(b);
c.enqueue(b);
}
}
Expected output: 123b
123bb
Actual output: 123
123
我调试了这个代码块:
public String toString(){
String a = "123";
for(Item item : this){
a = a + item;
}
return a;
}
并看到 "this" 持有 String a
的值。您是否要将 Item
class 更改为 Character
class,并在 a
上调用 toCharArray()
方法而不是使用 this
.这就是我所说的:
public String toString(){
String a = "123";
for(Character item : a.toCharArray()){
a = a + item;
}
return a;
}
您将进入循环并能够添加您想要的任何新字符。希望这有帮助。
您的 FIFOIterator
构造函数创建了一个新对象 curr 但没有将其设置为相同 class 的字段。因此,您的字段 curr
为空且 hasNext
returns false
.
改变
// Set attribute of node curr
public FIFOIterator(){
Node curr = first;
curr.item = first.item;
curr.next = first.next;
}
至
// Set attribute of node curr
public FIFOIterator(){
this.curr = first;
}
我在方法 toString() 中有一个 for-each 循环,它应该遍历通用 FIFO 队列中的元素(使用双向链表数据结构实现)并将队列中的项目连接到字符串 a 上,这返回到打印字符串的 enqueue(),它表示我的队列及其在 enqueue 调用后的内容。我的问题是,为什么 for-each 根本不是 executed/entered?
我尝试插入 System.out.print("Hi");在 for-each 里面,它没有打印出来。所以我假设某些代码块阻碍了它正常执行。
// FIFOQueue is implemented using the structure double linked list (DLL)
// Generic, iterable
import java.util.Iterator;
import java.util.*;
public class FIFOQueueDLL<Item> implements Iterable<Item>{
private Node first;
private Node last;
private int length = 0;
// is the queue empty?
public boolean isEmpty(){
return length == 0;
}
private class Node{
Item item;
Node next;
Node previous;
}
// add an item
public void enqueue(Item n){
Node newnode = new Node();
newnode.item = n;
if(isEmpty()){
last = newnode;
} else {
first.previous = newnode;
}
newnode.next = first;
first = newnode;
length++;
System.out.println(this);
}
// remove and return the least recently added item
public Item dequeue(){
if(isEmpty()){
throw new NoSuchElementException();
}
Node t = last;
if(first == last){
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
t.previous = null;
length--;
System.out.println(this.toString(););
return t.item;
}
public String toString(){
String a = "123";
for(Item item : this){
a = a + item;
}
return a;
}
public Iterator<Item> iterator(){
return new FIFOIterator();
}
private class FIFOIterator implements Iterator<Item>{
// Declare attribute
Node curr;
// Set attribute of node curr
public FIFOIterator(){
Node curr = first;
curr.item = first.item;
curr.next = first.next;
}
//private int i = length;
public boolean hasNext(){
return curr != null;
}
public Item next(){
Item a = curr.item;
curr = curr.next;
return a;
}
}
public static void main(String[] args){
FIFOQueueDLL<Character> c = new FIFOQueueDLL<Character>();
char b = 'b';
c.enqueue(b);
c.enqueue(b);
}
}
Expected output: 123b
123bb
Actual output: 123
123
我调试了这个代码块:
public String toString(){
String a = "123";
for(Item item : this){
a = a + item;
}
return a;
}
并看到 "this" 持有 String a
的值。您是否要将 Item
class 更改为 Character
class,并在 a
上调用 toCharArray()
方法而不是使用 this
.这就是我所说的:
public String toString(){
String a = "123";
for(Character item : a.toCharArray()){
a = a + item;
}
return a;
}
您将进入循环并能够添加您想要的任何新字符。希望这有帮助。
您的 FIFOIterator
构造函数创建了一个新对象 curr 但没有将其设置为相同 class 的字段。因此,您的字段 curr
为空且 hasNext
returns false
.
改变
// Set attribute of node curr
public FIFOIterator(){
Node curr = first;
curr.item = first.item;
curr.next = first.next;
}
至
// Set attribute of node curr
public FIFOIterator(){
this.curr = first;
}