Why is the value same ?should have been diffrent for each of the item . | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is the value same ?should have been diffrent for each of the item .

https://code.sololearn.com/c4pGj07KSpFl/?ref=app

7th May 2019, 3:18 PM
Yanne Tse
Yanne Tse - avatar
8 Answers
+ 1
See, when you pass an array as an Argument as a function like this, you actually don't pass the array itself but a copy of it. That copy is being modified,not the original. What you can do is to define a global array at the top of the class, so it can accessed from within the function: static item[] items = new item[20]; static void generate(){ //here use items just like it is a real argument }
7th May 2019, 4:50 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
I guess that a new "item" array is being created in the parameters of genarate(), a copy of your original array. You only modify this copy, not the original itself. I'd recommend to look into the concept of so called Pointers.
7th May 2019, 4:19 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
thanks I think I can figure now to do what I wanted ..for each item have a diffrent ammout because now once the ammout is created all the rest with the same name will share the same amount
7th May 2019, 4:40 PM
Yanne Tse
Yanne Tse - avatar
+ 1
I've try to use static item but still sane result so I was think in the loop just set item[i].ammout = random.next properly better this way
7th May 2019, 5:25 PM
Yanne Tse
Yanne Tse - avatar
+ 1
and delete the one in constructor
7th May 2019, 5:25 PM
Yanne Tse
Yanne Tse - avatar
7th May 2019, 5:36 PM
Yanne Tse
Yanne Tse - avatar
0
Did it
7th May 2019, 5:36 PM
Yanne Tse
Yanne Tse - avatar
0
Nice! Well done
7th May 2019, 6:09 PM
Jonas Schröter
Jonas Schröter - avatar