I am wondering how to use the iterator in a Stack class. How do I create a iterator class for it?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You could do:
Just get the
Iterator
viaiterator()
:Or alternatively, if you just want to print them all use the enhanced-for loop:
I am working on something that is implementing a stack using queues
Does that mean you are not using the Java
Stack
implementation? http://docs.oracle.com/javase/6/docs/api/java/util/Stack.html It is base on Vector not queues.If you are using the Java
Stack
implementation, you can use iterator like other answers. Otherwise, if that's a customStack
, you have to implement theIterable
interface. And then you can do something like other answers.Sounds like you implemented a custom stack class. Your "something" should implement the
Iterable
interface and provide an implementation ofIterator
.