On Aug 4, 7:39 pm, JKid314159 <[email protected]> wrote:
> Re:
> The Java Tutorials:
> Home Page > Collections > Interfaces > List Interface:
> Iterators
>
> Dear Java Programmer:public int indexOf(E e) {
> for (ListIterator<E> it = listIterator(); it.hasNext(); )
> if (e == null ? it.next() == null : e.equals(it.next()))
> return it.previousIndex();
> return -1; // Element not found
>
> }The if statement checks to see if current element is null?Stuck on this
> syntax. Please comment. Thank you.
You have here first inside parenthesis a condensed if operator, then
the if:
e = null ? it.next() == null : e.equals(it.next())
which means if e is null, returns the result of it.next() == null
if e is not null returns the result of e.equals(it.next())
It is equivalent to:
if (e = null) {
it.next() == null
} else {
e.equals(it.next()
}
Notice that the two statements:
it.next() == null
and
e.equals(it.next())
return true of false
So it is as if you write something like this:
e = null ? (true/false) : (true/false)
and then:
if (resultoftheternaryoperator) which is true or false depending on
the result of the ternary operator.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---