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.
Q15: 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;
}
Q18: 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.