Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help understanding a code snippet from the Mala Gupta book.

 
Sama Willson
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Apologies in advance if I am asking a dumb question. I have been having some difficulty understanding this particular code snippet from Mala Gupta(Chapter 5, pg:267) :

This is basically trying to distinguish when a for:each loop variable might change the value of an item of an array/collection.



In this case, the original collection doesn't change. But if we use a method to modify the value of an item , the original collection changes as well.

So,my question is, in case of Objects, the for:each loop only changes the original collection if we execute a method on it  and not when we assign a new object to it? Or when, we change the try to change the state through member variables?  What are some other cases where the original collection will remain unchanged and when it'll be changed?

Thanks in advance for the help.
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello sam,
i think you are asking for below 2 cases(method and loop):



actually during a loop iteration (via forEach) we are not referencing the object to the original refrence we do it like below:

because java is pass by value,changing or modifying refrence to e will not rerofits the original array.

but in case of method you are modifying the original refrence may be there also you can make a copy of array object and play with it instead of the original one.

and that is the reason why this for loop is recommended in code where possible.

hope it helps!
kind regards,
praveen  
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sama Willson wrote:Apologies in advance if I am asking a dumb question.

There is no such thing as a "dumb question" We are all here to learn and if you have some doubts or you don't understand a code snippet, it's in fact very smart to start a new topic and share your doubts/question

Sama Willson wrote:This is basically trying to distinguish when a for:each loop variable might change the value of an item of an array/collection.

In this topic the same question is answered given a different code snippet. Definitely worth reading! And if you still have questions/doubts, don't hesitate to let us know by simply hitting the Reply button.

Hope it helps!
Kind regards,
Roel
 
Sama Willson
Greenhorn
Posts: 22
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thanks for answering. The post that you directed me to was indeed helpful. And to answer your question(from the other post), after the loop is done iterating, OCA  OCA will be printed to the console. I also wrote a sample program late last night. It is very simple and I drew some pictures to help me understand what was happening. I am going to post them here, so that they might be helpful to others.




Thanks again for all the help!

PS: The line numbers included in the pictorial representation all correspond to the line numbers in the file attached named 'sample program'.
sample-program.png
[Thumbnail for sample-program.png]
pic-1.jpg
[Thumbnail for pic-1.jpg]
 
Sama Willson
Greenhorn
Posts: 22
Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding : Another picture that for some reason didn't post on the original reply.
Pic-2.jpg
[Thumbnail for Pic-2.jpg]
 
Sama Willson
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:i think you are asking for below 2 cases(method and loop):

Thanks Praveen , that was indeed very helpful.
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats my pleasure! sama.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic