protected int modCount = 0;
ArrayList(int initialSize)
IF initialSize<0 THENThrow exception "Invalid Size"
else
arrayElement= new Object[initialSize];
ArrayList()
Call ArrayList(defaultSize); // default-size is 10ArrayList(Collection<? extends E> C)
arrayElement = C.toArray();
size = arrayElement.length;
IF arrayElement.getClass() != Object[].class THEN
arrayElement = Arrays.copyOf(arrayElement, size, Object[].class);
ensureCapacity(int newCapacity)
modCount++oldCapacity = arrayElement.length;
IF newCapacity > oldCapacity THEN
Object[] oldData = arrayElement;
newCap = (oldCap *3)/2 +1;
IF newCap < newCapacity THEN
newCap=newCapacity;
ENDIF
END IF
arrayElement =Arrays.copyOf(arrayElement, newCap); // My own implementation is at the end.
add(E data)
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = data;
add(int index, E element)
IF index > size OR index < 0 THEN
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
ENDIF
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1, size - index); //shifting the right hand side elements by 1.
elementData[index] = element;
size++;
RangeCheck(int index)
IF index >= size THEN
throw new IndexOutOfBoundsException( "Index: "+index+", Size: "+size);
remove(int index)
RangeCheck(index)modCount++; // modification count
Object oldVal = elementData.get(index);
int numMoved = size - index - 1;
addAll(Collection<? extends E> c)
Object[] a = c.toArray();
length = a.length;
ensureCapacity(size + numNew);
System.arraycopy(a, 0, elementData, size, numNew);
size = size + numNew;
return numNew != 0;
........... to be continued (will add other details later)
No comments:
Post a Comment