1 / 32

FROM the AbstractList CLASS: public boolean add(Object o) { add(size(), o);

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

Télécharger la présentation

FROM the AbstractList CLASS: public boolean add(Object o) { add(size(), o);

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. WHAT’S THE DIFFERENCE BETWEEN AN ABSTRACT METHOD AND A METHOD WHOSE DEFINITION THROWS AN EXCEPTION? A 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).

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

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

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

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

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

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

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

  10. Here are two equivalent statements: ArrayList newList = (ArrayList)myList.clone( ); ArrayList newList = new ArrayList (myList);

  11. Clone Copy Constructor

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

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

  14. INHERITED FROM AbstractList: protected transient int modCount = 0;

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

  16. EACH ITERATOR CLASS IN THE JAVA COLLECTIONS FRAMEWORK HAS A FIELD: int expectedModCount = modCount;

  17. public Object next( ) { … checkForComodification( );

  18. final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }

  19. THE ITERATOR FAILS AS SOON AS IT IS DISCOVERED THAT ANOTHER OBJECT – EITHER ANOTHER ITERATOR OR THE ArrayList OBJECT ITSELF – HAS STRUCTURALLY MODIFIED THE ArrayList OBJECT.

  20. FOR THAT REASON, ITERATORS IN THE JAVA COLLECTIONS FRAMEWORK ARE CALLED FAIL-FAST ITERATORS

  21. FOR EXAMPLE, THE FOLLOWING CODE WILL THROW ConcurrentModificationException: public ModCountDriver( ) { ArrayList list = new ArrayList( ); list.add (“yes”); Iterator itr = list. iterator( ); list.add (“good”); itr.next( ); // exception thrown at this point } // default constructor Basically, once you start an iterator, you should not add or remove from the collection except through the iterator’s methods.

  22. APPLICATION: HIGH-PRECISION ARITHMETIC

  23. IN PUBLIC-KEY CRYPTOGRAPHY, THE INTEGERS ARE HUNDREDS OF DIGITS LONG.

  24. KEY FACTS: 1. TO GENERATE A VERY LONG INTEGER THAT IS PRIME, averageTime (n) IS O ((log n)3). IF n = 10200, (log10n)3 = 2003 = 8,000,000. 2. TO FACTOR A VERY LONG INTEGER THAT IS NOT PRIME, averageTime (n) is O (n 1/ 2). IF n = 10200, n1/2 = 10100. 3. GIVEN PRIMES p AND q, (p – 1)(q – 1) IS USED TO ENCODE A PUBLIC MESSAGE. 4. TO DECODE THE MESSAGE, p AND q MUST BE KNOWN.

  25. // Postcondition: this VeryLongInt is empty. public VeryLongInt( ); // Precondition: the string s consists of a sequence of // characters, with non-digit characters // ignored. There are no leading zeroes, // except for 0 itself, which has a single '0'. // The worstTime (n) is O (n). // Postcondition: this VeryLongInt has been initialized // from s. public VeryLongInt (String s);

  26. // Postcondition: a String representation of this // VeryLongInt has been returned. The // worstTime (n) is O (n). public String toString(); // Postcondition: The VeryLongInt has been // incremented by otherVeryLong. // The worstTime (n) is O (n). public void add (VeryLongInt otherVeryLong);

  27. GROUP EXERCISE: CONSTRUCT TWO VeryLongInteger OBJECTS WITH VALUES 345 AND 6789. PRINT OUT THEIR SUM. HINT: START AS FOLLOWS: VeryLongInteger very1 = new VeryLongInteger (“345”);

More Related