Question: // -- grow the array to requested capacity c, throw an exception if the requested // capacity c is smaller than the current size public
// -- grow the array to requested capacity c, throw an exception if the requested // capacity c is smaller than the current size public void ensureCapacity(int c) throws IllegalArgumentException;
I'm having trouble understanding how to write this
This is what I have so far
public void ensureCapacity(int c) throws IllegalArgumentException { if (size() >= data.length) { @SuppressWarnings("unchecked") T[] newData = (T[]) (new Object[size * 2 + 1]); System.arraycopy(data, 0, newData, 0, size); data = newData; } else { throw new IllegalArgumentException(); } }
Running code for Java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
