繁体   English   中英

为什么我在Java代码中得到此输出?

[英]Why I am getting this output in my Java code?

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

使用迭代器的Stack的第一个元素列表显然无法正常工作。 我不知道为什么。 这是我在Stack类中用于Iterator的代码:

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

这是我的Driver类中的代码,似乎有问题:

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

这里有所有的课程:

堆栈: http//pastebin.com/2HVLVHuM

队列类: http : //pastebin.com/3q537kHW

学生班: http//pastebin.com/UnBB7kPA

驱动程序类别: http//pastebin.com/yeA34MNd

我只能在堆栈类中写代码。 这样做的重点是使用队列实现堆栈。 希望这可以帮助

您需要在Student类中添加toString()方法。 迭代器正常工作,但是System.out.println()不知道如何显示Student

像这样向Student班级添加一些内容...

public String toString(){
    return name;
}

这样,当您调用System.out.println() ,它可以输出一个实数值。 当您调用System.out.println(Object) ,它总是尝试输出toString()值。 如果未定义此方法,它将输出对象的java ID,即您所看到的。

首先,请查看nneonneo的答案,以了解next()方法中的转换是否不正确。

其次,您的Iterator实现不正确。

next()在你的迭代函数返回元素current将其设定为后current.next

在迭代的最后一个元素上调用next()之后, hasNext()应该返回false。 但事实并非如此,因为current仍然指向您刚刚返回的元素。 因此,您将再次调用next() 在此方法中, current = current.next会将current设置为null ,然后将其返回。 既然hasNext是真实的,那应该不会发生,对吧?

出于同样的原因,堆栈的第一要素也缺失了:您将current设置为堆栈的顶部元素,但是在输出任何内容之前,您已经切换到current = current.next 您应该在完成输出执行此操作。

currentStack迭代器被定义为Node<Student> 您可以使用强制转换从next()方法返回current

因此, next()返回一个Node<Student> (类型转换为Student ),而不是实际的Student 由于Node可能没有toString方法,因此您将获得默认输出( Stack$Node@<addr> )。

要进行修复,请从next()返回类似current.item (假设存储在Node中的item称为item )。

使用迭代器的Stack的第一个元素列表显然无法正常工作。 我不知道为什么。

因为您的迭代器正在返回Node<Student>而不是学生。 问题出在:

return (Student)current;

您可能试图这样做,但是出现了Incompatible Type错误:

return current;

因此,您尝试通过投射进行修复。 问题在于节点不是学生。 一个节点包含一个学生。 您需要返回该节点包含的学生。

尝试这个:

    return current.data;

不需要强制转换,因为编译器知道node的“数据”成员是Student,因为您声明的当前类型为Node<Student> 这将解决您的学生打印错误的问题。 但是,正如@Konstantin指出的那样,您的迭代器仍然损坏。 您需要将current的值保存在一个临时变量中,移动current,然后返回该临时变量。 这是一种可能的实现:

    public Student next() {
        if (current == null) throw new NoSuchElementException();
        Node<Student> result = current;
        current = current.next;
        return result.data;
    }

[结语]

您确实需要查看泛型教程 从上面粘贴的代码尚不清楚,但在粘贴bin代码中很明显您正在使用Student作为类型参数。 这是非常不标准和令人困惑的。 惯例是使用大写字母-通常为T。您应该像下面这样声明stack。 在所有使用Student的地方,都用T代替。

public class Stack <T> implements Iterable<T>{   // good

代替

public class Stack <Student> implements Iterable<Student>{ // bad

T表示稍后要确定的某种类型,您可以将Stack与任何类型的对象一起使用。 仅当您实际创建堆栈时才使用Student(或其他任何方式)

public static void main(String [] args)
{
    Stack<Student> x = new Stack<Student>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM