boolean with setter & getter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

boolean with setter & getter

Hello, maybe you can help? My Output should be at the number 1 = true, but it gives me a false back. You know why? public void setFinal(boolean[] finalStates) { // TODO Auto-generated method stub boolean[] result = new boolean[3]; for(int i =0; i <3; i++) { if(i==1) { result[i] = true; } else { result[i] = false; } } } public boolean[] getFinal() { // TODO Auto-generated method stub boolean[] result = new boolean[3]; setFinal(result); return result; My Output is always: false false false Correct output should be: false true false

15th May 2020, 11:10 AM
Azad Er
Azad Er - avatar
2 Answers
+ 1
You give the array result as parameter but creating a new one and changes its values and then returns it without initialization to the first result variable. So just don't create the new variable in the function setFinal but use the parameter finalStates
15th May 2020, 11:18 AM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
0
Thank you so much.
15th May 2020, 11:25 AM
Azad Er
Azad Er - avatar