Write a new method for the ArrayIntList
class named indexOf
that accepts an integer parameter representing an element value and returns the index of the first occurrence of that value in the list.
If the value is not in the list, it should return -1
.
For example, if an ArrayIntList
variable named list
stores the following values:
[42, 7, -9, 14, 8, 39, 42, 8, 19, 0]
Then the call of list.indexOf(8)
should return 4
.
The call of list.indexOf(2)
should return -1
because the value 2
is not found in the list.
Assume you are adding to the ArrayIntList
class with following members:
public class ArrayIntList {
private int[] elementData;
private int size;
// construct a new empty list of capacity 10
public ArrayIntList() { ... }
// add to end of list
public void add(int value) { ... }
// throw ArrayIndexOutOfBoundsException if index not between min/max inclusive
private void checkIndex(int index, int min, int max) { ... }
// ensure that elementData.length >= capacity
private void ensureCapacity(int capacity) { ... }
...
// your code goes here
}