840 likes | 933 Views
CHAPTER 5 ArrayLists. FROM THE AbstractList CLASS: public boolean add(Object o) { add(size(), o); return true ; } abstract public Object get( int index); public void add( int index, Object element) { throw new UnsupportedOperationException(); }.
E N D
CHAPTER 5 ArrayLists
FROM THE AbstractList CLASS: public boolean add(Object o) { add(size(), o); return true; } abstract public Object get(int index); public void add(int index, Object element) { throw new UnsupportedOperationException(); }
WHAT’S THE DIFFERENCE BETWEEN AN ABSTRACT METHOD AND A METHOD WHOSE DEFINITION THROWS AN EXCEPTION? ANY SUBCLASS OF AbstractList MUST DEFINE THE get METHOD IN ORDER TO BE INSTANTIABLE, BUT NEED NOT DEFINE THE TWO-PARAMETER add METHOD AS LONG AS THAT METHOD IS NOT INVOKED (AND THE ONE-PARAMETER add METHOD IS NOT INVOKED).
FIELDS IN THE ArrayList CLASS private transient Object[ ] elementData; // “transient” means that the array elementData need // not be saved if the ArrayList object is serialized. The // individual elements will be saved, but not the array. privateint size;
// Postcondition: this ArrayList object has been initialized // to be empty and with a capacity given // by initialCapacity. public ArrayList (int initialCapacity) { elementData = new Object [initialCapacity]; } // constructor with int parameter
// Postcondition: this ArrayList object has been initialized // to be empty. public ArrayList ( ) { this (10); }
// Postcondition: o has been appended to this ArrayList // object and true has been returned. The // averageTime(n) is constant and // worstTime(n) is O (n). publicboolean add (Object o) { ensureCapacity (size + 1); elementData [size++] = o; returntrue; }
public void ensureCapacity(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { // Increase the capacity by at least 50%, // and copy the old array to the new array. } }
public void ensureCapacity(int minCapacity) { modCount++; // discussed below int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3) / 2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; elementData = new Object[newCapacity]; System.arraycopy(oldData, 0, elementData, 0, size); } }
public Object clone() { try { ArrayList v = (ArrayList)super.clone(); // copies size v.elementData = new Object[size]; System.arraycopy(elementData, 0, v.elementData, 0, size); v.modCount = 0; // the modCount field is // discussed later return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } }
// Postcondition: this ArrayList has been initialized to a // copy of c. public ArrayList(Collection c) { this((c.size()*110)/100); // Allow 10% room for growth Iterator i = c.iterator( ); while (i.hasNext( )) elementData[size++] = i.next(); } NOTE: THIS IS CALLED THE COPY CONSTRUCTOR.
HERE ARE TWO WAYS TO CREATE A COPY OFmyList: ArrayList newList = (ArrayList)myList.clone( ); ArrayList newList = new ArrayList (myList); SUPPOSE myList HAS 3 ELEMENTS:
Clone A1 A2 A3 Copy Constructor A1 A2 A3 null null null null null null null A1 A2 A3 nullnullnullnullnullnullnull
ITERATORS – NOT NEEDED FORArrayListS for (int j = 0; j < myList.size( ); j++) gui.println (myList.get (j));
BUT ITERATORS ARE LEGAL: Iterator itr = myList.iterator( ); while (itr.hasNext( )) gui.println (itr.next( ));
INHERITED FROMAbstractList: protected transient int modCount = 0;
THE modCount FIELD IS INCREMENTED EVERY TIME THE ArrayList IS STRUCTURALLY MODIFIED, THAT IS, WITH AN INSERTION OR REMOVAL.