/** {@inheritDoc} */ @Override public void add(int index, T element) { if (index == size) { // Fast-path: append onto the end of the list. add(element); } else if (index < 0 || size < index) { throw new IndexOutOfBoundsException(String.valueOf(index)); } else { // Slow-path: the list needs to expand and insert. // Do this the naive way, callers shouldn't abuse // this class by entering this code path. // add(null); // expand the list by one for (int oldIdx = size - 2; index <= oldIdx; oldIdx--) set(oldIdx + 1, get(oldIdx)); set(index, element); } }
@Override public void add(int index, T element) { if (index == size) { // Fast-path: append onto the end of the list. add(element); } else if (index < 0 || size < index) { throw new IndexOutOfBoundsException(String.valueOf(index)); } else { // Slow-path: the list needs to expand and insert. // Do this the naive way, callers shouldn't abuse // this class by entering this code path. // add(null); // expand the list by one for (int oldIdx = size - 2; index <= oldIdx; oldIdx--) set(oldIdx + 1, get(oldIdx)); set(index, element); } }
/** {@inheritDoc} */ @Override public void add(int index, T element) { if (index == size) { // Fast-path: append onto the end of the list. add(element); } else if (index < 0 || size < index) { throw new IndexOutOfBoundsException(String.valueOf(index)); } else { // Slow-path: the list needs to expand and insert. // Do this the naive way, callers shouldn't abuse // this class by entering this code path. // add(null); // expand the list by one for (int oldIdx = size - 2; index <= oldIdx; oldIdx--) set(oldIdx + 1, get(oldIdx)); set(index, element); } }
@Override public T remove(int index) { if (index == size - 1) { // Fast-path: remove the last element. T[] blockRef = directory[toDirectoryIndex(index)]; int blockIdx = toBlockIndex(index); T old = blockRef[blockIdx]; blockRef[blockIdx] = null; size--; if (0 < tailBlkIdx) tailBlkIdx--; else resetTailBlock(); return old; } else if (index < 0 || size <= index) { throw new IndexOutOfBoundsException(String.valueOf(index)); } else { // Slow-path: the list needs to contract and remove. // Do this the naive way, callers shouldn't abuse // this class by entering this code path. // T old = get(index); for (; index < size - 1; index++) set(index, get(index + 1)); set(size - 1, null); size--; resetTailBlock(); return old; } }