hi,
here is the code sample which is using : operator to call some class
methods.
for ( classname obj:somemethod() ) // here is the problem in
understanding
{
-----------------------
------------------------
-----------------------
}
regards
madni
Mike Schilling - 12 Jun 2006 12:55 GMT
> hi,
>
[quoted text clipped - 9 lines]
> -----------------------
> }
See the discussion of "enhanced for statement" in
http://java.sun.com/features/2003/05/bloch_qa.html.
Peter Van Weert - 12 Jun 2006 12:56 GMT
http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
> hi,
>
[quoted text clipped - 13 lines]
>
> madni
Jussi Piitulainen - 12 Jun 2006 13:09 GMT
> here is the code sample which is using : operator to call some class
> methods.
>
> for ( classname obj:somemethod() ) // here is the problem in
Here's an authoritative explanation:
<http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html>
I knew it was new in Java 1.5, so I googled for what's new in Java
1.5., and there it was; "for" might be a difficult word to find.
The mere answer is that
for (c o : m()) { ... }
iterates over a collection of values returned by the method m, binding
o to each of them in turn. The collection can be an array or a
Collection.
I recommmend spaces around the colon.
Adam Maass - 12 Jun 2006 14:18 GMT
> hi,
>
[quoted text clipped - 9 lines]
> -----------------------
> }
That's the syntax for the 'foreach' style for loop.
somemethod() returns a Collection (or other Iterable) or an array. classname
is the name of the class contained in such Collection or array. obj is the
loop variable. The code executes the body of the loop once for each element
in the array, supplying the current element in the variable obj.
-- Adam Maass