Write a new method for the ArrayIntList
class named stutter
that doubles the size of the list by replacing every integer in the list with two of that integer.
For example, if an ArrayIntList
variable named list
stores the following elements:
[1, 8, 19, 4, 17]
and we make the call of list.stutter();
it should store the following sequence of integers after the call:
[1, 1, 8, 8, 19, 19, 4, 4, 17, 17]
It is your responsibility to make sure that the list's internal array has enough capacity to accommodate the newly added elements.
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 IndexOutOfBoundsException 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
}