chapter 5 arraylists

84
CHAPTER 5 ArrayLists

Upload: wing-terrell

Post on 01-Jan-2016

34 views

Category:

Documents


2 download

DESCRIPTION

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(); }. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CHAPTER 5 ArrayLists

CHAPTER 5

ArrayLists

Page 2: CHAPTER 5 ArrayLists

Collection List

AbstractList

ArrayList LinkedList (Chapter 5) (Chapter 6)

Page 3: CHAPTER 5 ArrayLists

HERE ARE SOME METHOD

DESCRIPTIONS IN THE List

INTERFACE:

Page 4: CHAPTER 5 ArrayLists

// Precondition: 0 <= index < size( ). Otherwise,// IndexOutOfBoundsException will be// thrown.// Postcondition: the element at position index has// been returned.Object get (int index);

Page 5: CHAPTER 5 ArrayLists

// Precondition: 0 <= index < size( ). Otherwise,// IndexOutOfBoundsException will be// thrown.// Postcondition: element has replaced the element that// was at index before this call,// and that previous occupant has been// returned.Object set (int index, Object element);

Page 6: CHAPTER 5 ArrayLists

// Precondition: 0 <= index < size( ). Otherwise,// IndexOutOfBoundsException will be// thrown.// Postcondition: if elem does occur in this List, the index// of the first occurrence of elem has been// returned. Otherwise, -1 has been// returned. int indexOf (Object elem);

Page 7: CHAPTER 5 ArrayLists

// Precondition: 0 <= index <= size( ). Otherwise,// IndexOutOfBoundsException will be// thrown.// Postcondition: element has been inserted at// position index and every element that// was at a position >= index before this// call is now at the next higher position.void add (int index, Object element);

Page 8: CHAPTER 5 ArrayLists

// Precondition: 0 <= index < size( ). Otherwise,// IndexOutOfBoundsException will be// thrown.// Postcondition: the element that was at position index// in this List before this call has been// removed, every element that was in a// position > index before this call is now// at the next lower position, and the// removed element has been returned. Object remove (int index);

Page 9: CHAPTER 5 ArrayLists

ArrayList: A CLASSY ARRAY

RANDOM ACCESS OF AN ELEMENTFROM ITS INDEX:public Object get (int index),public void set (int index, Object element)

INSERTION OR REMOVAL OF ANELEMENT AT A GIVEN INDEX:public void add (int index, Object element)public Object remove (int index)

Page 10: CHAPTER 5 ArrayLists

AUTOMATIC RESIZING AFTERpublic void add (int index, Object element)public boolean add (Object o) // adds at back

Page 11: CHAPTER 5 ArrayLists

USER’S VIEW OF ArrayList CLASS:

Page 12: CHAPTER 5 ArrayLists

1. public ArrayList (int initialCapacity)2. public ArrayList( )3. public ArrayList (Collection c)4. public boolean add (Object o) // inserts at back5. public void add (int index, Object element)6. public boolean addAll (Collection c)7. public boolean addAll (int index, Collection c)8. public void clear( ) // worstTime (n) is O (n)9. public Object clone( )10.public boolean contains (Object elem)11.public boolean containsAll (Collection c)

Page 13: CHAPTER 5 ArrayLists

12. public void ensureCapacity (int minCapacity)13. public boolean equals (Object o) 14. public Object get (int index) // worstTime(n) is

// constant15. public int hashCode( ) 16. public int indexOf (Object elem)17. public boolean isEmpty( ) 18. public Iterator iterator( ) 19. public int lastIndexOf (Object elem)20. public ListIterator listIterator( ) 21. public ListIterator listIterator (final int index)22. public boolean remove (Object o)

Page 14: CHAPTER 5 ArrayLists

23. public Object remove (int index)24. public boolean removeAll (Collection c)25. public boolean retainAll (Collection c) 26. public Object set (int index, Object element)27. public int size( )28. public List subList (int fromIndex, int toIndex)29. public Object[ ] toArray( )30. public Object[ ] toArray (Object[ ] a)31. public String toString()32. public void trimToSize( )

Page 15: CHAPTER 5 ArrayLists

EXAMPLE: HERE IS A processInput (String s) METHODTHAT STARTS BY CONVERTING s TO int n AND THEN

0. CONSTRUCTS AN ArrayList THAT HOLDS UP TO nELEMENTS. public ArrayList (int initialCapacity);

1. IN A LOOP WITH i GOING FROM 0 TO n – 1,APPENDS new Double (i) TO THE ArrayList.public boolean add (Object o);

2. INSERTS new Double (1.4) AT INDEX n / 3.public void add (int index, Object element);

3. REMOVES THE ELEMENT AT INDEX 2n / 3.public Object remove (int index);

4. MULTIPLIES THE MIDDLE ELEMENT BY 3.5.public Object get (int index) ;public Object set (int index, Object element);

5. PRINTS OUT THE ArrayList; public String toString();

Page 16: CHAPTER 5 ArrayLists

public void processInput (String s) { int n = Integer.parseInt (s);

Page 17: CHAPTER 5 ArrayLists

public void processInput (String s) { int n = Integer.parseInt (s); ArrayList myList = new ArrayList (n);

Page 18: CHAPTER 5 ArrayLists

public void processInput (String s) { int n = Integer.parseInt (s); ArrayList myList = new ArrayList (n); for (int i = 0; i < n; i++)

Page 19: CHAPTER 5 ArrayLists

public void processInput (String s) { int n = Integer.parseInt (s); ArrayList myList = new ArrayList (n); for (int i = 0; i < n; i++) myList.add (new Double (i));

Page 20: CHAPTER 5 ArrayLists

public void processInput (String s) { int n = Integer.parseInt (s); ArrayList myList = new ArrayList (n); for (int i = 0; i < n; i++) myList.add (new Double (i)); myList.add (n / 3, new Double (1.4));

Page 21: CHAPTER 5 ArrayLists

public void processInput (String s) { int n = Integer.parseInt (s); ArrayList myList = new ArrayList (n); for (int i = 0; i < n; i++) myList.add (new Double (i)); myList.add (n / 3, new Double (1.4)); myList.remove (2 * n / 3);

Page 22: CHAPTER 5 ArrayLists

public void processInput (String s) {

int n = Integer.parseInt (s);ArrayList myList = new ArrayList (n);

for (int i = 0; i < n; i++)myList.add (new Double (i));

myList.add (n / 3, new Double (1.4));myList.remove (2 * n / 3);double d = ((Double)myList.get (n / 2)).doubleValue( )

* 3.5;

Page 23: CHAPTER 5 ArrayLists

public void processInput (String s) {

int n = Integer.parseInt (s);ArrayList myList = new ArrayList (n);

for (int i = 0; i < n; i++)myList.add (new Double (i));

myList.add (n / 3, new Double (1.4));myList.remove (2 * n / 3);double d = ((Double)myList.get (n / 2)).doubleValue()

* 3.5;myList.set (n / 2, new Double (d));

Page 24: CHAPTER 5 ArrayLists

public void processInput (String s) {

int n = Integer.parseInt (s);ArrayList myList = new ArrayList (n);

for (int i = 0; i < n; i++)myList.add (new Double (i));

myList.add (n / 3, new Double (1.4));myList.remove (2 * n / 3);

double d = ((Double)myList.get (n / 2)).doubleValue( )* 3.5;

myList.set (n / 2, new Double (d));gui.println (myList) ;

} // method processInput

Page 25: CHAPTER 5 ArrayLists

IF THE INPUT IS 10, THE OUTPUT IS

[0.0, 1.0, 2.0, 1.4, 3.0, 14.0, 6.0, 7.0, 8.0, 9.0]

Page 26: CHAPTER 5 ArrayLists

THE ArrayList CLASS HEADING:

public class ArrayList extends AbstractList implements List, Cloneable, java.io.Serializable

Page 27: 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(); }

Page 28: CHAPTER 5 ArrayLists

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

Page 29: CHAPTER 5 ArrayLists

ArrayLists ARE Serializable:

THEY CAN BE SAVED TO DISK AS A

SERIES OF BYTES, AND CAN LATER

BE READ FROM DISK (DE-

SERIALIZED).

Page 30: CHAPTER 5 ArrayLists

ArrayLists ARE Cloneable: // Postcondition: a distinct copy of this ArrayList has been // returned. The worstTime (n) is O (n). public Object clone( );

Page 31: CHAPTER 5 ArrayLists

ArrayList myList1 = new ArrayList( ), myList2 = new ArrayList( ), myList3 = new ArrayList( );

myList1.add (“Kona”);myList2 = myList1; // two references to same ArrayListmyList3 = (ArrayList)myList1.clone( ); // references to different ArrayLists // that are initially equal

Page 32: CHAPTER 5 ArrayLists

myList1 myList2 myList3

Kona

Kona

Page 33: CHAPTER 5 ArrayLists

ArrayList myList1 = new ArrayList( ), myList2 = new ArrayList( ), myList3 = new ArrayList( );

myList1.add (“Kona”);myList2 = myList1; // two references to same ArrayListmyList3 = (ArrayList)myList1.clone( ); // references to different ArrayLists // that are initially equal

myList2.add (“Kamuela”);myList3.add (“Kihei”);

Page 34: CHAPTER 5 ArrayLists

myList1

myList2

myList3

Kona, Kamuela

Kona, Kihei

Page 35: CHAPTER 5 ArrayLists

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. 

private int size;

Page 36: CHAPTER 5 ArrayLists

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

Page 37: CHAPTER 5 ArrayLists

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

Page 38: CHAPTER 5 ArrayLists

// 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).public boolean add (Object o) {  ensureCapacity (size + 1);

elementData [size++] = o;

return true; }

Page 39: CHAPTER 5 ArrayLists

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.

  }}

Page 40: CHAPTER 5 ArrayLists

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

}}

Page 41: CHAPTER 5 ArrayLists

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

Page 42: CHAPTER 5 ArrayLists

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

Page 43: CHAPTER 5 ArrayLists

HERE ARE TWO WAYS TO CREATE A

COPY OF myList:

 

ArrayList newList = (ArrayList)myList.clone( );

ArrayList newList = new ArrayList (myList);

SUPPOSE myList HAS 3 ELEMENTS: 

Page 44: CHAPTER 5 ArrayLists

Clone

Copy Constructor

A1 A2 A3

A1 A2 A3 null null null null null null nullA1 A2 A3 null null null null null null null

Page 45: CHAPTER 5 ArrayLists

EXERCISE: COMPLETE THE

DEFINITION OF THE FOLLOWING

METHOD:

Page 46: CHAPTER 5 ArrayLists

public boolean addAll(Collection c) {

modCount++;int numNew = c.size();ensureCapacity(size + numNew);

Iterator e = c.iterator();

return numNew != 0;}

Page 47: CHAPTER 5 ArrayLists

ITERATORS – NOT NEEDED FOR ArrayListS

for (int j = 0; j < myList.size( ); j++)

gui.println (myList.get (j));

Page 48: CHAPTER 5 ArrayLists

BUT ITERATORS ARE LEGAL:

Iterator itr = myList.iterator( );

while (itr.hasNext( ))

gui.println (itr.next( ));

Page 49: CHAPTER 5 ArrayLists

INHERITED FROM AbstractList:

 

protected transient int modCount = 0;

Page 50: CHAPTER 5 ArrayLists

THE modCount FIELD IS INCREMENTED

EVERY TIME THE ArrayList IS

STRUCTURALLY MODIFIED, THAT IS,

WITH AN INSERTION OR REMOVAL.

Page 51: CHAPTER 5 ArrayLists

EACH ITERATOR CLASS IN THE JAVA

COLLECTIONS FRAMEWORK HAS A

FIELD:

int expectedModCount = modCount;

Page 52: CHAPTER 5 ArrayLists

public Object next( ) {

checkForComodification( );

Page 53: CHAPTER 5 ArrayLists

final void checkForComodification() {

if (modCount != expectedModCount)

throw new ConcurrentModificationException();

}

Page 54: CHAPTER 5 ArrayLists

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.

Page 55: CHAPTER 5 ArrayLists

FOR THAT REASON, ITERATORS IN

THE JAVA COLLECTIONS

FRAMEWORK ARE CALLED

FAIL-FAST ITERATORS

Page 56: CHAPTER 5 ArrayLists

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

Page 57: CHAPTER 5 ArrayLists

BASICALLY, ONCE YOU CONSTRUCT

AN ITERATOR, YOU SHOULD NOT ADD

OR REMOVE FROM THE COLLECTION

EXCEPT THROUGH THE ITERATOR’S

METHODS.

Page 58: CHAPTER 5 ArrayLists

EXERCISE: WHAT ARE THE VALUES OF

modCount AND expectedModCount WHEN

THE next( ) METHOD IS CALLED IN THE

ABOVE CONSTRUCTOR?

Page 59: CHAPTER 5 ArrayLists

APPLICATION

HIGH-PRECISION ARITHMETIC

Page 60: CHAPTER 5 ArrayLists

IN PUBLIC-KEY CRYPTOGRAPHY, THE

INTEGERS ARE HUNDREDS OF DIGITS

LONG.

Page 61: CHAPTER 5 ArrayLists

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.

Page 62: CHAPTER 5 ArrayLists

2. TO FACTOR A VERY LONG

INTEGER THAT IS NOT PRIME,

averageTime(n) IS ABOUT n 1/ 2.

IF n = 10200, n1/2 = 10100.

Page 63: CHAPTER 5 ArrayLists

3. GIVEN PRIMES p AND q,

(p – 1)(q – 1) IS USED TO

ENCODE A PUBLIC MESSAGE.

Page 64: CHAPTER 5 ArrayLists

4. TO DECODE THE MESSAGE, p

AND q MUST BE KNOWN.

Page 65: CHAPTER 5 ArrayLists

WE WILL NOW DEVELOP A VeryLongInt

CLASS TO HANDLE VERY LONG

INTEGERS.

IN THE METHOD DESCRIPTIONS, n

REFERS TO THE NUMBER OF DIGITS IN

THE CALLING OBJECT.

Page 66: CHAPTER 5 ArrayLists

// 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);  

Page 67: CHAPTER 5 ArrayLists

// Postcondition: a String representation of this// VeryLongInt has been returned. The// worstTime (n) is O (n).public String toString(); 

 // Postcondition: This VeryLongInt has been// incremented by otherVeryLong.// The worstTime (n) is O (n).public void add (VeryLongInt otherVeryLong); 

Page 68: CHAPTER 5 ArrayLists

VeryLongInt Applet:

http://www.cs.lafayette.edu/~collinsw/verylong/long.html

Page 69: CHAPTER 5 ArrayLists

FOR EXAMPLE, THE FOLLOWING

CODE CONSTRUCTS TWO

VeryLongInt OBJECTS WITH VALUES

345 AND 6789 AND PRINTS OUT THEIR

SUM. 

Page 70: CHAPTER 5 ArrayLists

VeryLongInt very1 = new VeryLongInt (“345”);

VeryLongInt very2 = new VeryLongInt (“6789”);

very1.add (very2);

gui.println (very1); // = gui.println (very1.toString( )); 

Page 71: CHAPTER 5 ArrayLists

FIELDS AND METHOD DEFINITIONS

FOR THE VeryLongInt CLASS

THE ONLY FIELD IS:

ArrayList digits; // holds all of the digits // in the VeryLongInt

Page 72: CHAPTER 5 ArrayLists

public VeryLongInt( ) { 

final int INITIAL_CAPACITY = 500;

digits = new ArrayList (INITIAL_CAPACITY); } // default constructor

Page 73: CHAPTER 5 ArrayLists

public VeryLongInt (String s) {

// FOR EACH CHARACTER c IN s,

//

IF ‘0’ <= c <= ‘9’

//

APPEND (c – ‘0’) TO digits.

} // constructor with string parameter

SUPPOSE s = “3x79”. WHAT WILL digits

CONTAIN?

Page 74: CHAPTER 5 ArrayLists

public String toString( ) {  final String EMPTY_STRING = "";  String s = EMPTY_STRING;  for (int i = 0; i < digits.size(); i++) s += digits.get (i); return s;  } // method toString

Page 75: CHAPTER 5 ArrayLists

FOR THE add METHOD:

VeryLongInt a1, a2;

// a1 = {3, 8, 7, 4, 9, 8}

// a2 = {5, 3, 6, 4}

a1.add (a2);

Page 76: CHAPTER 5 ArrayLists

LOOP 6 TIMES:

0: 8 + 4 = 12 APPEND 2, AND THE CARRY IS 1.

1: 9 + 6 + 1 (THE CARRY) = 16 APPEND 6 AND THE CARRY IS 1.

...

WE END UP WITH 268293, SO REVERSE.

Page 77: CHAPTER 5 ArrayLists

public void add (VeryLongInt otherVeryLong) { 

final int BASE = 10; 

int largerSize,

partialSum, carry = 0; 

VeryLongInt sum = new VeryLongInt(); 

if (digits.size() > otherVeryLong.digits.size())

largerSize = digits.size();

else

largerSize = otherVeryLong.digits.size();

Page 78: CHAPTER 5 ArrayLists

for (int i = 0; i < largerSize; i++) { 

// Add the ith least significant digit in the

// calling object, the ith least significant digit

// in otherVeryLong, and the carry.

// Append that sum % 10 to sum.digits

// The new carry is that sum / 10.

} // for 

if (carry == 1)

sum.digits.add (new Integer (carry));

Collections.reverse (sum.digits);

digits = sum.digits;} // method add

Page 79: CHAPTER 5 ArrayLists

for (int i = 0; i < largerSize; i++) { 

partialSum = least (i) + otherVeryLong.least (i) + carry;

sum.digits.add (new Integer ( partialSum % BASE));

carry = partialSum / BASE;

} // for 

if (carry == 1)

sum.digits.add (new Integer (carry));

Collections.reverse (sum.digits);

digits = sum.digits;} // method add

Page 80: CHAPTER 5 ArrayLists

SUPPOSE THE VeryLongInt OBJECT HAS THE VALUE 13579. THEN

least (0) RETURNS 9

least (1) RETURNS 7

least (2) RETURNS 5

least (3) RETURNS 3

least (4) RETURNS 1

least (5) RETURNS 0

Page 81: CHAPTER 5 ArrayLists

// Postcondition: If i >= digits.size(), 0 has been returned; // else the ith least significant digit in// digits has been returned. The rightmost // digit is the 0th least significant digit.private int least (int i) {  if (i >= digits.size()) return 0; else

return ((Integer)(digits.get ( digits.size() - i - 1))).intValue ();

} // least

Page 82: CHAPTER 5 ArrayLists

HOW LONG DOES THE add METHOD

TAKE, ON AVERAGE? RECALL THAT n

REFERS TO THE NUMBER OF DIGITS.

Page 83: CHAPTER 5 ArrayLists

THE least METHOD TAKES CONSTANT

TIME, AND APPENDING TO AN ArrayList

TAKES CONSTANT TIME, ON

AVERAGE. SO THE AVERAGE TIME

FOR add DEPENDS ONLY ON THE

NUMBER OF ITERATIONS OF THE for

LOOP: n.

THE averageTime(n) IS LINEAR IN n.

Page 84: CHAPTER 5 ArrayLists

EXERCISE: FOR THE FOLLOWING

ADDITION, SHOW THE FINAL

CONTENTS OF a1.digits.elementData:

// a1 = {3, 8, 7, 4, 9, 8}

// a2 = {5, 3, 6, 4}

a1.add (a2);