Originally posted by Hauldren Collider
View Post

/* * @(#)ArrayList.java 1.56 06/04/21 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util; public class ArrayList<E> { private Object[] elementData; private int size; public ArrayList(int initialCapacity) { this.elementData = new Object[initialCapacity]; } public E get(int index) { return (E) elementData[index]; } public void set(int index, E element) { elementData[index] = element; } public boolean add(E e) { ensureCapacity(size + 1); elementData[size++] = e; return true; } public void ensureCapacity(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } } public E remove(int index) { E oldValue = (E) elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work return oldValue; } }
Comment