Iterable arrays

R

Roedy Green

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?
 
C

Chris ( Val )

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

Eric Sosman

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)
 
R

Roedy Green

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

Eric Sosman

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?"
 
D

Daniel Pitts

Roedy said:
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?
 
M

Mike Schilling

Daniel Pitts said:
Roedy said:
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?

I'll speculate that the generated bytecode makes a copy of "arr" into a
temporay, so that nothing untoward happens.
 
M

Mike Schilling

Mike Schilling said:
Daniel Pitts said:
Roedy said:
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?

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

Patricia Shanahan

Mike said:
Daniel Pitts said:
Roedy said:
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?

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
 
W

Wayne

Roedy said:
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
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top