2

When I pass the array "bucky" to the change function and in the change function if I use an enhanced for loop, the variable counter gives the error "The value of the local variable counter is not used"

But if I put the body of the for loop in an System.out.println(counter+=5) the error does not appear

class apples{
 public static void main(String[] args) {
  int bucky[]={3,4,5,6,7};

  change(bucky);
  }

  public static void change(int x[]){
    for(int counter: x){
     counter+=5;
    }
  }
}

Why does this code give the error I mentioned above since I'm using the counter variable in the enhanced for loop?

Edit - my objective is to change values of the array inside the "change" function and return it back to main.

  • 1
    Try and print the content of `bucky` in your `main` after calling `change`. You'll see why it thinks `counter` is not used. – Federico klez Culloca Jul 12 '22 at 09:36
  • You're "using" but without effect, `counter` is just a local variable that'll take thev values or the array one by one, but won't change the array values. It you're printing it then you need the variable, but for now there is on real use of having that variable – azro Jul 12 '22 at 09:40
  • How do I change the array values using the enhanced for loop? – Thulitha Gurusinghe Jul 12 '22 at 09:43
  • 1
    @ThulithaGurusinghe basically you don't. You use a normal loop. – Federico klez Culloca Jul 12 '22 at 09:44
  • 2
    I suggest you read [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) to have a more informed understanding on what you can and can't do with arrays and reference types more generally. – Federico klez Culloca Jul 12 '22 at 09:46
  • @ThulithaGurusinghe , what your exact question, you want to change array values & increment each value by 5? – Ashish Patil Jul 12 '22 at 09:49
  • @AshishPatil I want to change the values of the array – Thulitha Gurusinghe Jul 12 '22 at 09:51
  • @ThulithaGurusinghe, you want to change value of array and return it back to main, is that question? please let us know so that answer can be shared accordingly if you are still looking for answer. – Ashish Patil Jul 12 '22 at 09:52
  • Yes, I want to change values in the change function and return it back to main – Thulitha Gurusinghe Jul 12 '22 at 09:54
  • 1
    @ThulithaGurusinghe , shared answer. please check and confirm – Ashish Patil Jul 12 '22 at 10:01
  • You should follow the Java Naming Conventions: class names are written in PascalCase. – MC Emperor Jul 12 '22 at 10:50
  • And the general consensus is to place the array brackets (`[` and `]`) behind the *component type*, and not behind the variable name. Placing the brackets behind the variable name is borrowed from C. – MC Emperor Jul 12 '22 at 10:51

1 Answers1

2

Question is about updating original array values, then you have to use normal for loop so that each index values can be updated:

class apples{
 public static void main(String[] args) {
  int bucky[]={3,4,5,6,7};

  bucky = change(bucky);
  for(int b:bucky) {
        System.out.println(b);
    }
  }

  public static void change(int x[]){
    for (int i = 0; i < x.length; i++) {
        x[i]=x[i]+5;
    }
    return x;
  }
}
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36