320 likes | 503 Views
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
E N D
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? 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).
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) { 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(); }
Here are two equivalent statements: ArrayList newList = (ArrayList)myList.clone( ); ArrayList newList = new ArrayList (myList);
Clone Copy Constructor
ITERATORS – NOT NEEDED FOR ArrayLists 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 FROM AbstractList: protected transient int modCount = 0;
THE modCount FIELD IS INCREMENTED EVERY TIME THE ArrayList IS STRUCTURALLY MODIFIED, THAT IS, WITH AN INSERTION OR REMOVAL.
EACH ITERATOR CLASS IN THE JAVA COLLECTIONS FRAMEWORK HAS A FIELD: int expectedModCount = modCount;
public Object next( ) { … checkForComodification( );
final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }
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.
FOR THAT REASON, ITERATORS IN THE JAVA COLLECTIONS FRAMEWORK ARE CALLED FAIL-FAST ITERATORS
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.
IN PUBLIC-KEY CRYPTOGRAPHY, THE INTEGERS ARE HUNDREDS OF DIGITS LONG.
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.
// 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);
// 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);
GROUP EXERCISE: CONSTRUCT TWO VeryLongInteger OBJECTS WITH VALUES 345 AND 6789. PRINT OUT THEIR SUM. HINT: START AS FOLLOWS: VeryLongInteger very1 = new VeryLongInteger (“345”);