jdk 1.5 enhanced for loop, Iterable and arrays

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.
 
S

sks

Remi Bastide said:
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
}


Iterable<String> method() {
String[] strings = new String[] { "a", "b", "c" };
return Arrays.asList(strings);
}

* untested * !
 
R

Remi Bastide

sks said:
I impl = new MyImplementation();

for(String s : impl.f() ) {
// Do something with s
}


Iterable<String> method() {
String[] strings = new String[] { "a", "b", "c" };
return Arrays.asList(strings);
}

Yes, but in this case, you do allocate a new ArrayList on each call.
Do you think this occurs "behind the scene" when using the enhanced
for loop with native arrays, or is there another compiler trick which
does not require allocating a new ArrayList ?
 
T

Tony Morris

Remi Bastide said:
sks said:
I impl = new MyImplementation();

for(String s : impl.f() ) {
// Do something with s
}


Iterable<String> method() {
String[] strings = new String[] { "a", "b", "c" };
return Arrays.asList(strings);
}

Yes, but in this case, you do allocate a new ArrayList on each call.
Do you think this occurs "behind the scene" when using the enhanced
for loop with native arrays, or is there another compiler trick which
does not require allocating a new ArrayList ?

Assuming impl.f() is the method that is referred to that returns the
List<String>, the call to the method occurs only once for each execution of
the foreach loop.
Here are two points of reference:
1. The JLS 3.0 proposed.
2. Analysis of the compiled bytecode.
 
C

Chris Uppal

Remi said:
return Arrays.asList(strings);
[...]
Yes, but in this case, you do allocate a new ArrayList on each call.

Probably not worth worrying about.

Do you think this occurs "behind the scene" when using the enhanced
for loop with native arrays, or is there another compiler trick which
does not require allocating a new ArrayList ?

The compiler generates different code for iterating over a List or an array. In
both cases the resulting bytecodes are the same whether you use the shortcut
form or write an explicit loop in the "traditional" form. It does not
introduce any additional overhead.

-- chris
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
Enhanced for loop in java

Old form of looping.

for (int i=0; i < nameArray.length; i++)
{
System.out.println("Name: " + array);
}
With Java SE 5.0. an enhanced looping would be like

for (String name : nameArray)
{
System.out.println("Name: " + name);
}

where nameArray is an array of Strings

Hope it did add value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top