jdk 1.5 enhanced for loop, Iterable and arrays

Discussion in 'Java' started by Remi Bastide, Feb 10, 2005.

  1. Remi Bastide

    Remi Bastide Guest

    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.
     
    Remi Bastide, Feb 10, 2005
    #1
    1. Advertising

  2. Remi Bastide

    sks Guest

    "Remi Bastide" <> wrote in message
    news:...
    > 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 * !
     
    sks, Feb 10, 2005
    #2
    1. Advertising

  3. Remi Bastide

    Remi Bastide Guest

    "sks" <11.co.uk> wrote:

    >
    >"Remi Bastide" <> wrote in message
    >news:...

    ...
    >> 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 ?
     
    Remi Bastide, Feb 11, 2005
    #3
  4. Remi Bastide

    Tony Morris Guest

    "Remi Bastide" <> wrote in message
    news:...
    > "sks" <11.co.uk> wrote:
    >
    > >
    > >"Remi Bastide" <> wrote in message
    > >news:...

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

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Feb 11, 2005
    #4
  5. Remi Bastide

    Chris Uppal Guest

    Remi Bastide wrote:

    > > 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
     
    Chris Uppal, Feb 11, 2005
    #5
  6. Remi Bastide

    elizas

    Joined:
    Jan 27, 2010
    Messages:
    27
    Location:
    India
    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.
     
    elizas, Mar 29, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Koenig
    Replies:
    46
    Views:
    1,009
    Peter Maas
    Feb 24, 2004
  2. visionset

    enhanced for loop

    visionset, May 4, 2007, in forum: Java
    Replies:
    13
    Views:
    593
  3. Replies:
    2
    Views:
    382
    James Stroud
    Dec 10, 2008
  4. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page