我想知道如何在Stack类中使用迭代器。如何为其创建迭代器类?
只需获取 via 迭代器():Iterator
迭代器()
Iterator
Stack<YourObject> stack = ... Iterator<YourObject> iter = stack.iterator(); while (iter.hasNext()){ System.out.println(iter.next()); }
或者,如果您只想打印所有打印它们,请使用增强型循环:
for(YourObject obj : stack) { System.out.println(obj); }
你可以做:
for (Iterator<MyObject> iterator = stack.iterator(); iterator.hasNext();) { MyObject myObject = iterator.next(); myObject.doStuff(); }