Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Iterable arrays

Thread view: 
Roedy Green - 25 Oct 2007 14:19 GMT
You can use arrays in for:each.  I presume that means somehow arrays
must have an iterator method that produces an Iterator.  

What are the methods of arrays?  

Or is this just fudged as a special case by the compiler?
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Chris ( Val ) - 25 Oct 2007 15:04 GMT
On Oct 25, 11:19 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> You can use arrays in for:each.  I presume that means somehow arrays
> must have an iterator method that produces an Iterator.  
>
> What are the methods of arrays?  
>
> Or is this just fudged as a special case by the compiler?

I'm not sure of the internal workings of it, but I read
recently that it was specifically designed to work with
arrays and collections.

--
Chris
Eric Sosman - 25 Oct 2007 16:52 GMT
Roedy Green wrote On 10/25/07 09:19,:
> You can use arrays in for:each.  I presume that means somehow arrays
> must have an iterator method that produces an Iterator.  
>
> What are the methods of arrays?  
>
> Or is this just fudged as a special case by the compiler?

   Special-cased by the compiler.  (How do I know?
I wrote foreach loops for a List<String> and for
a String[], and looked at the generated bytecode.)

   For the List<String>, the generated code calls
the .iterator() method and then uses .hasNext() and
.next() on the Iterator<String>.

   For the String[], the generated code is roughly
equivalent to

    for (int n = array.length, i = 0;  i < n;  ++i)

... which may be a hair more efficient than

    for (int i = 0;  i < array.length;  ++i)

Signature

Eric.Sosman@sun.com

Roedy Green - 25 Oct 2007 18:48 GMT
>    For the String[], the generated code is roughly
>equivalent to
>
>    for (int n = array.length, i = 0;  i < n;  ++i)

that's odd.  I've never noticed for:each iterating in reverse order.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Eric Sosman - 25 Oct 2007 21:26 GMT
Roedy Green wrote On 10/25/07 13:48,:

>>   For the String[], the generated code is roughly
>>equivalent to
>>
>>    for (int n = array.length, i = 0;  i < n;  ++i)
>
> that's odd.  I've never noticed for:each iterating in reverse order.

   "Reverse order?"

Signature

Eric.Sosman@sun.com

Roedy Green - 25 Oct 2007 23:04 GMT
>>>    for (int n = array.length, i = 0;  i < n;  ++i)
>>
>> that's odd.  I've never noticed for:each iterating in reverse order.
>
>    "Reverse order?"

sorry.  I read the first few chars and presumed you wrote:

for (int n = array.length-1; n>=0; n--)
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Daniel Pitts - 25 Oct 2007 22:18 GMT
>>    For the String[], the generated code is roughly
>> equivalent to
>>
>>     for (int n = array.length, i = 0;  i < n;  ++i)
>
> that's odd.  I've never noticed for:each iterating in reverse order.
I think you misread that.
it iterates i from 0 to n.  I recall hearing that array.length is a
no-no on JavaME, so perhaps they just use the same output for both cases?

I wonder what happens when you do something like this:
Object[] arr = new Object[2];
for (Object o: arr) {
   arr = new Object[0];
}

(I know I could test that, but I will in a bit.)
Anyone want to speculate before I test?

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Mike  Schilling - 26 Oct 2007 01:09 GMT
>>>    For the String[], the generated code is roughly
>>> equivalent to
[quoted text clipped - 14 lines]
> (I know I could test that, but I will in a bit.)
> Anyone want to speculate before I test?

I'll speculate that the generated bytecode makes a copy of "arr" into a
temporay, so that nothing untoward happens.
Mike  Schilling - 26 Oct 2007 01:12 GMT
>>>>    For the String[], the generated code is roughly
>>>> equivalent to
[quoted text clipped - 17 lines]
> I'll speculate that the generated bytecode makes a copy of "arr" into a
> temporary, so that nothing untoward happens.

Copy of the reference "arr", of course, not of the array itself.
Patricia Shanahan - 26 Oct 2007 03:25 GMT
Mike Schilling wrote:

>>>>    For the String[], the generated code is roughly
>>>> equivalent to
[quoted text clipped - 16 lines]
> I'll speculate that the generated bytecode makes a copy of "arr" into a
> temporay, so that nothing untoward happens.

The JLS gives the equivalent basic for code. See
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2

And, yes, it does call for the array reference expression to be copied
to a temporary.

Patricia
Eric Sosman - 26 Oct 2007 13:35 GMT
> Mike Schilling wrote:
>>
[quoted text clipped - 6 lines]
> And, yes, it does call for the array reference expression to be copied
> to a temporary.

    Makes sense when you think about it, in light of

    for (String s : expensiveMethodReturningArray())

or even

    for (String s : methodWithSideEffectsReturningArray())

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Wayne - 26 Oct 2007 04:08 GMT
> You can use arrays in for:each.  I presume that means somehow arrays
> must have an iterator method that produces an Iterator.  
>
> What are the methods of arrays?  
>
> Or is this just fudged as a special case by the compiler?

It has nothing to do with fudge.  It's the caffeine in Java
that makes the arrays iterable.

(Okay, no more puns, I promise.)

-Wayne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.