I tried this block of code and it yields two different results
public class PassArrayToFunction {
public static void increase1(int[] arr){
for(int a: arr)
a += 1;
}
public static void increase2(int[] arr){
for(int i=0;i<arr.length;i++)
arr[i]++;
}
public static void main(String[] args) {
int arr[] = {1,2,3};
increase1(arr);
System.out.println(Arrays.toString(arr));
increase2(arr);
System.out.println(Arrays.toString(arr));
}
}
So is there any underlying difference between 2 ways to iterating an array because principle wise it's basically the same
I was expecting sbd to clarify passing array to function in JAVA
int a = a[i]; a += 1;to have the equivalent of first loop (obviously? incrementingawill not be reflected in the array [both cases])