Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
CHAPTER 5 ArrayLists PowerPoint Presentation
Download Presentation
CHAPTER 5 ArrayLists

CHAPTER 5 ArrayLists

80 Vues Download Presentation
Télécharger la présentation

CHAPTER 5 ArrayLists

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. CHAPTER 5 ArrayLists

  2. 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(); }

  3. 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).

  4. 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;

  5. // 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

  6. // Postcondition: this ArrayList object has been initialized // to be empty. public ArrayList ( ) { this (10); }

  7. // 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; }

  8. 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.   } }

  9. 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);   } }

  10. 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(); } }

  11. // 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.

  12. HERE ARE TWO WAYS TO CREATE A COPY OFmyList: ArrayList newList = (ArrayList)myList.clone( ); ArrayList newList = new ArrayList (myList); SUPPOSE myList HAS 3 ELEMENTS:

  13. Clone A1 A2 A3 Copy Constructor A1 A2 A3 null null null null null null null A1 A2 A3 nullnullnullnullnullnullnull

  14. ITERATORS – NOT NEEDED FORArrayListS for (int j = 0; j < myList.size( ); j++) gui.println (myList.get (j));

  15. BUT ITERATORS ARE LEGAL: Iterator itr = myList.iterator( ); while (itr.hasNext( )) gui.println (itr.next( ));

  16. INHERITED FROMAbstractList: protected transient int modCount = 0;

  17. THE modCount FIELD IS INCREMENTED EVERY TIME THE ArrayList IS STRUCTURALLY MODIFIED, THAT IS, WITH AN INSERTION OR REMOVAL.