Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 21 Implementing lists: array implementation.

Similar presentations

Presentation on theme: "Chapter 21 Implementing lists: array implementation."— Presentation transcript:

1 Chapter 21 Implementing lists: array implementation

2 This chapter discusses n Implementing the class List. n Implementations on a primitive language-provided structure called an array. n Several important relationships between classes used to effect various implementations.

3 Arrays n array: a structure composed of a contiguous sequence of variables, all of the same type. n Individual variables are identified by index values or indexes. n Index values are integers, (beginning with 0 in Java). n Components or elements are the variables that comprise the array. They are a primitive type ( char, int, boolean, double, etc.) or a reference type (reference to an object).

4 Arrays (cont.) n The length of the array is the number of component variables that comprise it. n With length n, the component variables are indexed 0 through n-1.

5 Arrays (cont.) n The length of an array is fixed when the array is created. n Since contiguous memory is allocated for the variables, accessing a particular variable in the array requires a constant amount of time independent of the array length.

6 Arrays (cont.) n Suppose each variable in a particular array may take up four bytes, and memory is allocated starting at location 100 n The address of the variable with index i is 100+4*i. (starting address)+(variable size)*(element index) n The calculation of the address requires constant time, independent of the index and independent of the size of the array.

7 Array objects n Arrays in Java are encapsulated in objects. Array objects have a public int component length. n The class of an array is determined by the type of its component variables. n The class is written as the component type followed by a pair of brackets. int[] Student[]

8 Defining arrays int[] grades; Student[] cs2125; int grades[]; Student cs2125[]; n The variable contains a reference to an array object. n We use a constructor to create the array object. grades = new int[5]; cs2125 = new Student[5];

9 Defining arrays (cont.)

10 The component variables of the arrays are initialized with standard default values. e.g. 0 for int, null for references. n The length given in the constructor need not (and generally should not) be a literal. cs2125 = new Student[studentCount]; grades = new int[4*cs2125.length];

11 Accessing array components A reference to the array is followed by an index value in brackets. grades[3] = 100; grades[4] = grades[3]/2; cs2125[0] = new Student(…); cs2125[0].payFees(100); int i; for(i = 0; i < grades.length; i=i+1) grades[i] = 100; The index value must be an int, non- negative, and less than the length of the array.

12 Accessing array components (cont.) If an array is referenced with an index that is negative or is not less than the array length, an ArrayIndexOutOfBoundsException is thrown.

13 BoundedList public abstract class BoundedList A list of objects with a fixed maximum size. protected BoundedList (int maxSize) Create a new BoundedList with a specified maximum size. require: maxSize >= 0 ensure: isEmpty(new BoundedList(n)) Since the class is abstract and an abstract constructor can be invoked only by a subclass, it is declared protected.

14 BoundedList (cont.) n Component variables: private Object[] elements; private int size; elements will hold the components of the array. size will hold the current length of the list (not the maximum number of elements).

15 BoundedList (cont.) protected BoundedList (int maxSize){ Require.condition(maxSize >= 0); elements = new Object[maxSize]; size = 0; } public int size() { return this.size; } public Object get (int i) { Require.condition(0 <= i && i < size); return element[i]; } public void append (Object obj) { Require.notNull(obj); Require.condition(size < elements.length); elements[size] = obj; size = size+1; }

16 BoundedList (cont.) public void add (int i, Object obj) { Require.notNull(obj); Require.condition( 0 <= i && i <= size); Require.condition(size < elements.length); int j; for (j = size-1; j >= i; j = j-1) elements[j+1] = element[j]; elements[i] = obj; size = size + 1; } public void remove (int i) { Require.condition(0 <= i && i < size); int j; for ( j = i; j < size-1; j = j+1) elements[j] = element[j+1]; size = size - 1; }

17 BoundedList (cont.) public void set (int i, Object obj) { Require.notNull(obj); Require.condition( 0 <= i && i < size); elements[i] = obj; } public void clear () { size = 0; }

18 BoundedList (cont.) The methods add (and remove ) shuffle a portion of the array up or down. n Elements must be moved starting with the last, or else elements will be overwritten.

19 BoundedList (cont.) n For instance, if we write for ( j = i; j < size; j= j+1) elements[j+1] = elements[j];

20 BoundedList (cont.) When elements are deleted with clear or remove, there is no need to set array components to null. n These elements are considered invalid; it does not matter what they contain.

21 BoundedList (cont.) public boolean isEmpty () { return this.size() == 0; } public int indexOf (Object obj) { int i; int n = this.size(); i = 0; while (i <n && !obj.equals(get(i))) i = i+1; if (i == n) return -1; else return i; }

22 BoundedList (cont.) public String toString () { String s = [; int n = this.size(); if (n > 0) { s = s + this.get(0).toString(); int i; for(i = 1; i < n; i = i+1) s = s +, + this.get(i).toString(); } s= s + ]; } public int maxSize () { return elements.length; }

23 The method copy n We make a shallow copy of the list. i.e. the original list and the copy reference the same list elements.

24 The method copy (cont.) public BoundedList copy() { int i; BoundedList theCopy = new BoundedList(elements.length); for (i = 0; i < size; i=i+1) { theCopy.elements[i] = this.elements[i]; theCopy.size = this.size; return theCopy; } //This doesnt work because BoundedList is an abstract class.

25 Java Interface Cloneable Object class has a method clone. protected Object clone () throws CloneNotSupportedException Create a copy of this Object. n This method has these properties s != s.clone() s.equals(s.clone()); s.clone() instanceof Student //of course we are implying that s is a Student. Since clone returns an object, it is often necessary to cast the result. Student s2 = (Student) s.clone();

26 Java Interface Cloneable (cont.) public interface Cloneable {} Any class that supports the method clone should implement this interface. If not, a CloneNotSupportedException is thrown. The method clone works essentially like this: if (this instanceof Cloneable) { Object copy = new instance of this class; for each component variable v of this copy.v = this.v; return copy; else throw CloneNotSupportedException;

27 Java Interface Cloneable (cont.) Implementing the interface does not require a class to implement any methods; the class inherits the method clone implemented in the class Object.

28 Java Interface Cloneable (cont.) If Java were built with multiple inheritance, Cloneable would be a class from which other objects could inherit and the method clone would be defined there.

29 Java Interface Cloneable (cont.) It simply turns on the method clone inherited from Object.

30 BoundedList public abstract class BoundedList implements Clonable { … public Object clone() { BoundedList theCopy = (BoundedList)super.clone(); …

31 BoundedList (cont.) n We may want a less shallow copy of the list so that we can manipulate the list and its clone independently.

32 BoundedList (cont.) public Object clone() { try { BoundedList theCopy = (BoundedList)super.clone(); theCopy.elements = (Object[])this.elements.clone(); return theCopy; } catch (CloneNotSupportedException e) { return null; }

33 Implementation n We need to cast the result explicitly to BoundedList or Object[ ] as appropriate. Javas scoping rules allow private components of the BoundedList theCopy to be accessed directly in the method. Since the Object method clone is specified as possibly throwing a CloneNotSupportedException, we must include a try statement and catch it, or include a throws clause in the specifications of the method.

34 Implementation (cont.) n In order to have an abstract class that creates a new object, we implement a method like this: public BoundedList copy () { return (BoundedList) this.clone(); } The object created by the call to super.clone() will be of the same run- time class as the object executing the call.

35 Abstract Constructor n An abstract method can be used like a constructor for creating objects in a superclass. n Sometimes it is called a factory method. n Subclasses implement the method and determine which actual concrete class to instantiate. public abstract BoundedList createList (int maxSize) Create a new BoundedList with a specified maximum size. require: maxSize >= 0 ensure: isEmpty(createList(n))

36 Abstract constructor (cont.)

37 public class BoundedStudentList extends BoundedList { … /** * Create a new BoundedStudent list * require: * maxSize >= 0 * ensure: * isEmpty(createList(n)) *createList(n) instanceof * BoundedStudentList */ public BoundedList createList (int maxsize) { return new BoundedStudentList (maxsize); } … }

38 Abstract constructor (cont.) public BoundedList copy() { int i; BoundedList theCopy = createList( this.element.size); for (i = 0; i < this.size; i=i+1) theCopy.elements[i]=this.elements[i]; theCopy.size = this.size; return theCopy; }

39 Advantages of arrays Elements can be accessed efficiently; get, append, and set all operate in constant time.

40 Limitations of arrays n The array is static. The client must have a good idea of the ultimate size of a list when the list is created. u Too large means wasted space. u Too small causes failure. Operations remove, add, and indexOf all are linear. n Conclusions u Not the best implementation for dynamic lists. u Often a good choice for static lists.

41 Dynamic arrays n A bounded list has a maximum size. n A dynamic list has no maximum size. n A static array can be made into a dynamic array by simply creating a bigger array when the original array is full.

42 Dynamic lists public void append (Object obj) { Require.notNull(obj); if (this.size == elements.length) { // need a bigger array Object[] newArray = new Object[2*elements.length]; //copy contents of old array to new int i; for(i = 0; i < elements.length; i=i+1) newArray[i] = elements[i]; elements = newArray; } elements[size] = obj; size = size +1; }/*append becomes a very expensive operation because there is the possibility of having to copy the entire array into a new array. (linear time) */

43 Vector class n Vector class is a container class that encapsulates a dynamic use of arrays. public Vector (); public Vector (int initialCapacity); public Vector (int initialCapacity, int capacityIncrement); New Vector (100,20) default initialCapacity = 10 default capacityIncrement = 0 capacityIncrement of 0 means it doubles everytime it outgrows capacity.

44 Wrapper n A class that adds functionality to or modifies the specification of another class. n A class that provides required functionality but lacks the required interface can be tailored to fit our needs by wrapping the class in a wrapper class. n Vector has all of the functionality and most of the methods of lists.

45 Wrapper specifications public Object elementAt(int index); The element at the given array index. public int indexOf (Object elem) Index of the given element; -1 if the array does not contain the element. public int size () The number of elements in the Vector. public Object clone() A new copy of the Vector. public void addElement (Object elem) Add the element to the end of the Vector; size is increased, and capacity is incremented if necessary. public void insertElementAt (Object obj, int index) Add the given element at the given array index, shifting elements up as necessary.

46 Wrapper specifications (cont.) public void removeElementAt (int index) Remove the element at the given index, and shift higher indexed elements down. public void setElementAt (Object obj, int index) Replace the element at the specified array index with the Objectt provided. public String toString() A string representation of this Vector, containing the String representation of each element. public void removeAllElements() Removes all the components from this Vector and sets its size to zero. n The time complexity of the methods is the same as with BoundedList.



49 Weve covered n Javas array classes n Object arrays used to implement a version of the class List n Cloning arrays n Solving the fixed array limitation u Vectors u Dynamic arrays u Wrappers n Time complexities.

50 Glossary

Download ppt "Chapter 21 Implementing lists: array implementation."

Similar presentations

Ads by Google