R
Remi Bastide
With the new jdk 1.5 enhanced for loop, one can treat native array and
collections in a similar way, which is nice, e.g.:
String[] array = {};
for(String s : array)
System.out.println(s);
List<String> list = new LinkedList<String>();
for(String s : list)
System.out.println(s);
However, it appears that a String[] cannot be cast to an
Iterable<String> ,
so for instance, it is incorrect to write
Iterable<String> f() {
return new String[]{};
}
What I'd really like to do is :
- Declaring an interface with methods that return something Iterable :
interface I {
Iterable<String> f();
}
- Having several implementations of I, some returning native arrays,
other returning collections
So that I could write:
I impl = new MyImplementation();
for(String s : impl.f() ) {
// Do something with s
}
Any help appreciated.
collections in a similar way, which is nice, e.g.:
String[] array = {};
for(String s : array)
System.out.println(s);
List<String> list = new LinkedList<String>();
for(String s : list)
System.out.println(s);
However, it appears that a String[] cannot be cast to an
Iterable<String> ,
so for instance, it is incorrect to write
Iterable<String> f() {
return new String[]{};
}
What I'd really like to do is :
- Declaring an interface with methods that return something Iterable :
interface I {
Iterable<String> f();
}
- Having several implementations of I, some returning native arrays,
other returning collections
So that I could write:
I impl = new MyImplementation();
for(String s : impl.f() ) {
// Do something with s
}
Any help appreciated.