public boolean contains(Object o) {
for (E x : this)
if (x.equals(o))
return true;
return false;
}
Can someone tell me
What exactly means
thisin this code?
It is always a reference to the current instance. I assume your class implements the Iterable
The loop is just a syntax sugar for the enhanced for statement. According to the specification (§14.14.2.):
for ({VariableModifier} UnannType VariableDeclaratorId : Expression) StatementThe type of the Expression must be Iterable or an array type (§10.1), or a compile-time error occurs.
If the type of
Expressionis a subtype ofIterable, then the translation is as follows.If the type of
Expressionis a subtype ofIterablefor some type argumentX, then letIbe the typejava.util.Iterator; otherwise, letIbe the raw typeIterator.The enhanced for statement is equivalent to a basic for statement of the form:
for (I #i = Expression.iterator(); #i.hasNext(); ) { {VariableModifier} TargetType Identifier = (TargetType) #i.next(); Statement }
Usually, a class implements the Iterable to provide to an API user the ability of being allowed to iterate over the internal collection hiding the actual implementation.
Can I write it without this and how?
for.