• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array passing in parameters?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know all arguements in Java are passed by value but what I don't get is why my values in my array are not changing when I call the method changeArray(); to my understanding ray is a reference pointing to the memory of three ints,so when I call the changeArray method I am passing a copy of the arr reference into that method and I am assigning the values to ray but at the same time both arr and ray are referring to the same block of memory which the array is stored,so my question how come when I print arr[1] it still prints 5 and not 22?

thanks


 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys it is doing what I say I forgot that I included ray = new int[3] sorry my bad
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting the answer you found.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across array initialisers? You may find them a fast and reliable way to create that sort of array:-You can shorten that even more, but only if the declaration and initialisation are kept together:-That guarantees that your array will be the right size and you won't accidentally initialise the wrong number of elements.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!