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.
Q21: Put the following code in order to create a method that will find the last occurrence of a target value and return the index of where that value is located.
public static int find (ArrayList<Integer> arr, int target) {
---
int loc = -1;
---
for (int i = 0; i < arr.size(); i++) {
---
if (arr.get(i) == target)
---
loc = i;
---
}
---
return loc;
}
public static int count (ArrayList<Integer> arr, int target) {
---
int num = 0;
---
for (int i = 0; i < arr.size(); i++) {
---
if (arr.get(i) == target)
---
num++;
---
}
---
return num;
}