Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add(). Elements not yet added do not exist until explicitly inserted.
Passing as argument - a copy of the reference to the instantiated ArrayList is passed to the method. This means that any changes made to the elements inside the method persist outside the method. The exception is if the argument is assigned to reference a different ArrayList inside the method.
Q24: What does the following code accomplish? Assume that alpha is a correctly declared ArrayList with non-default values and that the variable target contains a value.
Q26: What does the following code accomplish? Assume that alpha is a correctly declared ArrayList with non-default values and that the variable target contains a value.
public static double average (ArrayList<Integer> arr) {
---
int sum = 0;
int size = arr.size();
---
if (size == 0)
---
return 0.0;
---
for (int i : arr)
---
sum += i;
---
return (sum + 0.0) / size;
---
}