for iterator().hasNext() does the current element ...
# announcements
s
for iterator().hasNext() does the current element count as a true condition
🚫 3
r
Read the doc
hasNext()
returning
true
means that
next()
will work
s
eg if there is only a single element
as
s.forEach { println("it = $it") }
does not print the last element if there is more than 1 element, and does not print anything at all if there is only 1 element
g
What is s, what is forEach, where is an iterator?
s
Copy code
fun main() {
    val word = "ab"
    println("word = " + word)
    val s = stringToStack(word)
    println("stringToStack = $s")
    val i = s.iterator()
    while (i.hasNext()) println("next = " + i.next())
    s.forEach { println("it = $it") }
}
r
What’s
stringToStack
g
This code doesn't mean anything without context. Is it your own iterator implementation I suppose
If so, you just have a bug therr
Iterator is just an interface with convention how it should behave, not a magic, you of course can implement it with incorrect behavior, but all stdlib iterators do not have this problem
s
Copy code
word = ab
stringToStack = [a, b]
next = a
it = a
g
What is that? It's not even valid Kotlin syntax
s
Copy code
fun stringToStack(str: String): Stack<String> {
    val deq = Stack<String>()
    var i = 0
    while (i < str.length) deq.addLast(str[i++].toString())
    return deq
}
g
Do you understand that your comments without context just mean nothing for other people? so there is no chance to help you using it, only if one try to guess what does this code mean
g
Because you asked recently about implementation details of iterator, I suppose you implement your own iterator and this iterator implementation is incorrect
r
What is this
Why don’t you just use
LinkedList
s
i dont know
so theres
main
,
stringToStack
,
Stack
and
LinkedList
r
What the hell are you doing
g
LinkedList is Java class, as I remember Smallville tried to implement this for Native, but I'm not sure
So, is your question "why my custom iterator has a bug"? Did you even tried to debug it?
s
the documentation for it literally just states
Returns true if the iteration has more elements.
r
Yes. That is what the documentation states. So what’s your point?
s
how am i supposed to know that "Returns true if the iteration has more elements." actually means "`hasNext()` returning
true
means that
next()
will work"
r
s
which implies the
next
element, not the
current
element
r
What do you think
next
means
It’s supposed to return the next element, not the current one. I mean it’s written right there in the name
s
the next element after the current element
r
Yes, that’s what next is supposed to return
Your implementation of next returns the current value.
s
so it should be this?
Copy code
/**
             * Returns the next element in the iteration.
             */
            override fun next(): T? {
                node = node?.next
                if (node != null) {
                    return node?.value
                }
                throw Exception("No Such Element")
            }
thus return...
Copy code
word = ab
stringToStack = [a, b]
next = b
it = b
for
Copy code
fun main() {
    val word = "ab"
    println("word = " + word)
    val s = stringToStack(word)
    println("stringToStack = $s")
    val i = s.iterator()
    while (i.hasNext()) println("next = " + i.next())
    s.forEach { println("it = $it") }
}
?
g
which implies the
next
element, not the
current
element
There is no such thing as "current" element in iterator, there is only "next" or nothing
r
Yeah I said that your implementation of
next
was wrong I never said it was the only thing wrong
s
so when i.next() is first called, it will return the first element then the next element on subsequent calls?
instead of the second element then the next on subsequent calls?
g
Yes, otherwise it would be impossible to get the first element of iterator
s
ok
so it is this?
Copy code
override fun hasNext(): Boolean = node != null
            override fun next(): T? {
                val var0 = node?.value
                node = node?.next
                return var0
            }
do kotlin iterators throw an exception if next() is called when hasNext() returns false or does that only happen in JVM
for example,
Copy code
if (tokenList.peek() == null) {
            try {
                tokenList.pop()
            } catch (e: java.util.NoSuchElementException) {
                return null
            }
        }
with
tokenList
originally being of type
ArrayDeque
r
Read the doc
s
Copy code
Throws:
    NoSuchElementException - if this deque is empty