Download presentation
Presentation is loading. Please wait.
Published byJesse Jordan Modified over 9 years ago
1
Announcements I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class not today as I am still waiting on the written test grades in the last class I will go through a few topics based on your suggestions, hence, if you want a specific topic to be discussed/reviewed, email me before Oct 22, 2011 will also discuss the final 1
2
Arrays [notes Chapter 8a], [AJ 6] 2
3
What is an Array? an array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, it is fixed. from, http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.htmlhttp://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html 3
4
How to Use Arrays? declaring an array int[] anArray; // an array of integersOR int anArray[]; you can use other types: byte, short, long, float, double, boolean, char, String anArray = new int[10]; // create an array of integers anArray[0] = 100; // initialize first element anArray[1] = 200; // initialize second element System.out.println("Element 1 at index 0: "+anArray[0]); System.out.println("Element 2 at index 1: "+anArray[1]); 4
5
Why use Arrays? 5
6
Implementing List using Array the List interface is generic hence, the type parameter T in List, which captures the type of the elements of the list to use this interface, the client has to provide a non primitive type as argument i.e. List represents a list containing Double objects note that, our List interface is different from the interface java.util.List for simplicity, it contains fewer methods and some of the methods are specified slightly differently 6
7
BoundedArrayList Class in this implementation, the maximum size of the list is bound, later we will see how to make the list grow unboundedly 7
8
note that, BoundedArrayList implements the List interface means, we have to provide an implementation for each method specified in the List interface the List interface extends the Iterable interface Means, we have to provide an implementation for the single method iterator specified in the Iterable 8
9
Constructors public BoundedArrayList(int capacity) { this.size = 0; this.elements = new Object[capacity]; } //default constructor, delegating constructor above public BoundedArrayList() { this(BoundedArrayList.CAPACITY); } 9
10
The Copy Constructor we have to create a new array in the copy constructor to copy the content of the original array to the new one public BoundedArrayList(BoundedArrayList list) { this.size = list.size; this.elements = new Object[list.elements.length]; for (int i = 0; i < list.size; i++) { this.elements[i] = list.elements[i]; } Don’t need to throw an ArrayIndexOutOfBoundException (see the class invariant) 10
11
instead of manually creating a new array and copying the content from the old one to the new one, we can delegate to the Arrays class and use the static method copyOf(original, length), which is part of the java.util package public BoundedArrayList(BoundedArrayList list) { this.size = list.size; this.elements = Arrays.copyOf(list.elements, list.elements.length); } 11
12
getSize Method public int getSize() { return this.size; } 12
13
get Method get(index) returns the element of the list with the given index the return type of the method is T whereas the base type of the array elements is Object means, we have to cast public T get(int index) throws IndexOutOfBoundsException { if (index >= 0 && index < this.size) { return (T) this.elements[i]; } else { throw new IndexOutOfBoundsException("Invalid index"); } 13
14
add Method public boolean add(T element) { boolean added; if (this.size == this.elements.length) { added = false; } else { this.elements[this.size] = element; this.size++; added = true; } return added; } 14
15
contains Method public boolean contains(T element) { boolean found = false; for (int i = 0; i < this.size; i++) { found = found || this.elements[i].equals(element); } return found; } loop invariant: found == ∃ 0 ≤ j < i : this.elements[j].equals(element) 15
16
if we want to exit once we have found the element public boolean contains(T element) { boolean found = false; for (int i = 0; i < this.size && !found; i++) { found = found || this.elements[i].equals(element); } return found; } found = this.elements[i].equals(element); 16
17
remove Method loop invariant for the second loop ∀ i f < j < i : this.elements[j] has been copied to this.elements[j − 1] 17
18
equals Method loop invariant for the second loop equal == this.size() == other.size() && ∀ 0 j < i : this.elements[j].equals(other.elements[j]) 18
19
hashCode Method public int hashCode() { final int BASE = 31; int hashCode = 0; for (int i = 0; i < this.size; i++) { hashCode = BASE * hashCode + this.elements[i].hashCode(); } return hashCode; } loop invariant: hashCode == ∑_i<j<this.size 31 j × this.elements[j].hashCode() 19
20
hashCode Revised all class must provide a hashCode() method it digests the data stored in an instance of the class into a single hash value (identifier) and used by other code when storing or manipulating the instance we must override hashCode() in a way so that it behaves in a way consistent with the object's equals() method that is, an object must report the same hash value when equals() returns true. the implementation of hashCode() may differ, which means, two unequal objects having different hashes is desirable but not mandatory 20
21
in a nutshell: if x.equals(y) returns true then x.hashCode() and y.hashCode() return the same integer for all x and y different from null the opposite is not required, but may improve the performance of methods in some classes 21
22
we must override hashCode() in a way so that it behaves in a way consistent with the object's equals() method that is, an object must report the same hash value when equals() returns true. the implementation of hashCode() may differ, which means, two unequal objects having different hashes is desirable but not mandatory 22
23
toString Method public String toString() { StringBuffer representation = new StringBuffer("["); if (this.size != 0) { for (int i = 0; i < this.size - 1; i++) { representation.append(this.elements[i]); representation.append(", "); } representation.append(this.elements[this.size - 1]); } representation.append("]"); return representation.toString(); } 23
24
iterator Method public Iterator iterator() { return new BoundedArrayListIterator (this.elements); } remember, the iterator method in the Iterable interface returns an object of type Iterator Iterator is an interface (we cannot create instances), hence, we will need to return an instance of a class that implements the Iterator interface we need to develop a class BoundedArrayListIterator that implements the Iterator interface that will return an instance of the class 24
25
/* * class invariant: this.next >= 0 && * for all 0 <= i < this.elements.length : this.elements[i] instanceof T */ 25
26
Unbounded Size public boolean add(T element) { if (this.size == this.elements.length) { this.elements = Array.copyOf(this.elements, this.elements.length * 2); } this.elements[this.size] = element; this.size++; return true; } 26
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.