• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

For-Each Loops  RSS feed

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I just learned for-each loops yesterday and I understand that it initializes a variable that is the first element of the array. But I don't know why in the picture that piece of code is an error. Please help, thanks.
cs.PNG
[Thumbnail for cs.PNG]
 
Bartender
Posts: 5307
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"element" is a copy of a built in type so changing it does not change the value in the array. This is the way it works, it is not an error.

Also note that this doesn't work as you expected either
Even though "element" is now a local copy of a reference to a Double, a Double is immutable so you can't change its value. When you assign 9.9 to it what you are doing is creating a new Double with the value 9.9 and assigning a reference to that to "element", which only changes the local copy of the "element". It now no longer refers to something in "ary".
 
Ana Yo
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, why is the output 33 and not 90?

 
Ana Yo
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:"element" is a copy of a built in type so changing it does not change the value in the array. This is the way it works, it is not an error.

Also note that this doesn't work as you expected either
Even though "element" is now a local copy of a reference to a Double, a Double is immutable so you can't change its value. When you assign 9.9 to it what you are doing is creating a new Double with the value 9.9 and assigning a reference to that to "element", which only changes the local copy of the "element". It now no longer refers to something in "ary".



our teacher is trying to shows us that the for each loop is not valid but i don't know why
 
Saloon Keeper
Posts: 2086
76
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:Also, why is the output 33 and not 90?



try this:
 
Carey Brown
Bartender
Posts: 5307
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want to change the value of an array element you'd need to use the old style for() loop.
 
salvin francis
Saloon Keeper
Posts: 2086
76
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:...our teacher is trying to shows us that the for each loop is not valid but i don't know why


If you want to change the data in an array, then yes, its not possible with the for each loop.  This is because you do not directly access the array in the for each loop
 
Ana Yo
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our teacher gave us the example so it does output 33 but I don't understand why

 
Ana Yo
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't need to correct it, I just want to know the logic behind it and why it outputted 33. I thought it would output 90.
 
Carey Brown
Bartender
Posts: 5307
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 11 you create a LOCAL variable 'x' that contains a COPY of the value passed in. On line 13 you modify the COPY, not the original.
 
Sheriff
Posts: 12816
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like y'all are not on the same page, with someone talking about one thing and other talking about slightly different things. Let me see if I can sort this whole conversation out.

The code that OP posted had this comment:

and others seem to have assumed that the comment referred to some kind of runtime error.

Always consider context.

I don't think "ERROR" referred to a runtime error. Rather, I think it refers to a logic error, given the subsequent code that OP showed. I think the instructor's intent was to demonstrate how the loop variable in a for-each statement works.  In this case, assuming values is an array of double, the element variable is assigned the value of each element in the values array. The element variable is NOT a reference to array element but rather, a copy of an element's value. Anything you do to the element variable does not affect the corresponding element value in the values array.
 
Ana Yo
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is the array going to be {0,0,....} because it set each element to 0 ?^^
 
Junilu Lacar
Sheriff
Posts: 12816
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:So is the array going to be {0,0,....} because it set each element to 0 ?^^



No. Again, the variable element is simply a copy of a value in the array. Changing element DOES NOT change any value in the array.
 
Junilu Lacar
Sheriff
Posts: 12816
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps your confusion also stems from the variable being named "element", which in my opinion is a very poor choice by your instructor.  

This works the same way:

 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!