// returning an array from a method vs passing an array as parameter to a method
// passing an array as parameter to a method
public static int sumArray(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
// returning an array from a method
public static int[] assignArray(int length) {
int[] arr = new int[length];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
return arr;
}
////////////////////////////////////// in main
// returning an array from a method
int[] arr1 = assignArray(3);
int[] arr2 = assignArray(5);
for (int e : arr1) System.out.println(e); // 0 1 2
for (int e : arr2) System.out.println(e); // 0 1 2 3 4
// passing an array as parameter to a method
int[] array1 = {100, 200, 300};
int[] array2 = {50, 60, 70, 80, 90};
int sum1 = sumArray(array1);
int sum2 = sumArray(array2);
System.out.println("array1 sum=" + sum1); // 100 + 200 + 300 = 600
System.out.println("array2 sum=" + sum2); // 50 + 60 + 70 + 80 + 90 = 350