为什么我在Java代码中得到这个输出?
使用迭代器的堆栈元素的第一个列表显然不起作用。我不知道为什么。下面是我在堆栈类中为迭代器编写的代码:为什么我在Java代码中得到这个输出?,java,stack,queue,implementation,Java,Stack,Queue,Implementation,使用迭代器的堆栈元素的第一个列表显然不起作用。我不知道为什么。下面是我在堆栈类中为迭代器编写的代码: The student at the top of the stack is Gullion,Hailey Student Mcglothlen,Shizue is removed from the stack Here are all the elements of the Stack using an Iterator ---------------------------------
The student at the top of the stack is Gullion,Hailey
Student Mcglothlen,Shizue is removed from the stack
Here are all the elements of the Stack using an Iterator
--------------------------------------------------------
Stack$Node@3012db7c
Stack$Node@2607c28c
Stack$Node@477588d5
Stack$Node@756a7c99
Stack$Node@221a5d08
Stack$Node@70d1c9b5
Stack$Node@5d11c3f0
Stack$Node@3956f14c
Stack$Node@7afbd1fc
null
Here are all the elements in the Stack
--------------------------------------
Putney,John
Larkey,Ismael
Winkler,Isiah
Aceto,Liana
Hamill,Crissy
Caraway,Elmira
Gullion,Hailey
Rodrigez,Jonie
Madruga,Terrell
Williams,Diego
公共迭代器迭代器(){返回新的ListIterator();}
//迭代器不实现remove(),因为它是可选的
私有类ListIterator实现了迭代器{
私有节点当前=顶部;
公共布尔值hasNext(){
返回电流!=null;
}
public void remove(){
抛出新的UnsupportedOperationException();
@抑制警告(“未选中”)
公立学生下一个(){
如果(!hasNext())抛出新的NoSuchElementException();
当前=当前。下一步;
返回(学生)当前值;
}
}
下面是我的驱动程序类中的代码,这似乎是一个问题:
public Iterator<Student> iterator() { return new ListIterator(); }
// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator<Student> {
private Node<Student> current = top;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
@SuppressWarnings("unchecked")
public Student next() {
if (!hasNext()) throw new NoSuchElementException();
current = current.next;
return (Student)current;
}
}
System.out.println(“\n它们是使用迭代器的堆栈的所有元素”);
System.out.println(“-------------------------------------------------------------------------------------”);
迭代器iter=myStack.Iterator();
while(iter.hasNext())
System.out.println(iter.next());
以下是所有课程:
堆栈:
队列类别:
学生班级:
驾驶员等级:
我只能在STACK类中编写代码。其要点是使用队列实现堆栈。希望这有助于您需要在
Student
类中添加toString()
方法。迭代器工作正常,但System.out.println()
不知道如何显示Student
向学生
类添加如下内容
System.out.println("\nHere are all the elements of the Stack using an Iterator");
System.out.println("--------------------------------------------------------");
Iterator <Student> iter = myStack.iterator();
while (iter.hasNext() )
System.out.println(iter.next() );
因此,当您调用
System.out.println()
时,它可以输出一个实际值。当您调用System.out.println(Object)
时,它总是尝试输出toString()
value。如果未定义此方法,它将输出对象的java ID,这就是您所看到的。首先,请参阅NNONNEO对next()
方法中错误强制转换的回答
其次,迭代器实现是不正确的
迭代器中的next()
函数在将元素设置为current.next
后返回元素current
在迭代的最后一个元素上调用next()
后,hasNext()
应该返回false。但是它不会返回false,因为current
仍然指向您刚才返回的元素。因此您将调用next()
再次。在这个方法中,current=current。next
将current
设置为null
,然后返回它。这不应该发生,因为hasNext是真的,对吗
出于同样的原因,堆栈的第一个元素丢失了:您将
current
设置为堆栈的顶部元素,但在输出任何内容之前,您已经切换到current=current。下一步。您应该在输出后执行此操作。堆栈中的current
迭代器定义为Node
。使用强制转换从next()
方法返回current
因此,next()
返回一个节点(类型转换为Student
),而不是实际的Student
。由于Node
可能没有toString
方法,您将获得默认输出(Stack$Node@
)
要修复此问题,请从next()
返回类似current.item
的内容(假设存储在节点中的项被称为项
)
使用迭代器的堆栈元素的第一个列表显然不起作用。我不知道为什么
因为迭代器返回的是节点而不是学生。问题在于:
public String toString(){
return name;
}
您可能试图执行此操作,但出现了不兼容的类型错误:
return (Student)current;
因此,您尝试通过强制转换进行修复。问题是节点不是学生。节点包含学生。您需要返回节点包含的学生
试试这个:
return current;
不需要强制转换,因为编译器知道“数据”节点的成员是学生,因为您声明了类型为node
的current。这将解决学生打印不正确的问题。但是正如@Konstantin所指出的,您的迭代器仍然损坏。您需要将current的值保存在临时变量中,移动current,然后返回临时变量。以下是o可能的实施:
return current.data;
public Student next(){
如果(current==null)抛出新的NoTouchElementException();
节点结果=当前;
当前=当前。下一步;
返回结果数据;
}
[结语]
您确实需要查看。从上面粘贴的代码中看不清楚,但从粘贴箱代码中可以明显看出,您正在使用Student作为类型参数。这是非常不标准且容易混淆的。惯例是使用大写字母-通常是T。您应该像下面那样声明堆栈。无论在哪里使用Student,repl以T领先
public Student next() {
if (current == null) throw new NoSuchElementException();
Node<Student> result = current;
current = current.next;
return result.data;
}
公共类堆栈实现Iterable{//good
而不是
public class Stack <T> implements Iterable<T>{ // good
公共类堆栈实现了Iterable{//bad
T表示某种类型待以后决定,您可以将堆栈与任何类型的对象一起使用。只有当您实际创建堆栈时,才可以使用Student(或其他任何类型)
publicstaticvoidmain(字符串[]args)
{
堆栈x=新堆栈();
我有一个单独的学生类,这里没有显示它有一个toString方法。你有没有尝试过System.out.println(iter.next().toString());
-这是否给出了正确的输出?另外,你的while
循环实际上应该是while(iter.hasNext()!=null)
这样它就不会在最后一行写入null
,hasNext()方法应该检查current.next!=null
public class Stack <Student> implements Iterable<Student>{ // bad
public static void main(String [] args)
{
Stack<Student> x = new Stack<Student>();