Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Enhanced for loop array assignment?

 
Author
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following loop doesn't work:

for (int elt : myArray) {
elt = 7;
}

I decompiled this and found out why it doesn't work (in the technical sense) but I don't understand the rationale behind it's not working. Why did they set up the enhanced for loop this way?

Thanks.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is addressed (somewhat) in the 5.0 docs here - last paragraph. "These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases."

I guess they figured it was simplest to have a single mechanism for all uses of the enhanced for loop - so they invented the Iterable interface, and gave arrays an iterator() method. That allowed them to use the existing Iterator interface, which many existing classes already implement. I suppose they could have used the ListIterator interface instead, which would allow use of the set() method (among others) - but non-List Collections can't use that. And some Lists would thorw an UnupportedOperationException when you try to use the set method. The stack trace for this might be a bit confusing considering we can't see set() being explicitly called in the code. I think that if they'd allowed us to somehow use set() here, they'd have had to explain a number of additional special cases. Instead they chose to keep things simple, and limit the functionality of foreach. I suppose we might imagine that they could have drawn the line elsewhere, giving us a little more functionality for only a little bit more complexity, but that's unlikely to change anything now.
[ January 22, 2005: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
huh. looking at the docs for that for-each construct, it seems as if Java 5.0 has lifted the idea pretty much verbatim from Python 2.2 ... or vice versa. anyone know which of these were the chicken, and which the egg?

(note: for what it's worth, Python 2.2 was released in late December, 2001, and included the Iterator feature that these loops - in both Python and Java - appear to be based on, which was formally proposed as early as January of 2001. the same syntax was used in even earlier Python versions still to do much the same thing, but with stricter limitations and less flexibility. how long ago was Java 5.0 work begun...?)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, it would also have been quite confusing if it worked the way you expected. After all, "elt" is a new local variable - why would you expected a change to it being reflected in the array its value was copied from? There is no other place in Java where it works that way!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
huh. looking at the docs for that for-each construct, it seems as if Java 5.0 has lifted the idea pretty much verbatim from Python 2.2 ... or vice versa. anyone know which of these were the chicken, and which the egg?

Eh, who knows? The idea of foreach has been around for ages; I know it waas present in several UNIX shells well before Perl incorporated it. Iterators have been around a while too - I think they came from the Smalltalk community. Both of these may well have earlier antecedants, irrelevant for our purposes. Java got the Iterator interface as part of the Collections framework in, oh, 1997 maybe? And people have been asking for a foreach in Java since...ummm, a long time, though Sun was not terribly interested at first. Hurm, I can't seem to find such ancient RFE's in Sun's bug tracker - but I know I read requests for such things by 2000 at least. At the time, the requests came from people who liked the feature in Perl, and wanted something like it in Java. By early 2001, C# had come out, and it had a foreach loop of its own - one that looks a lot more like what Tiger eventually gave us than Python's does. I think this is what motivated Sun to eventually include enhanced for among the Tiger releases.
 
This tiny ad is wafer thin:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!