When declaring an ArrayList, the datatype stored in the container is specified inside of <>, and the data type must be the name of a class (no primitive data types)
The parameter to the method get represents the index in the ArrayList. The size of the ArrayList is the number of elements contained. If the ArrayList is initially empty, the size is 0.
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 one exception to this is if you assign the argument to reference a different ArrayList in memory.
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;
---
}