Upload
jesse-jordan
View
215
Download
1
Tags:
Embed Size (px)
Citation preview
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
Arrays
[notes Chapter 8a], [AJ 6]
2
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.html
3
How to Use Arrays? declaring an arrayint[] anArray; // an array of integers ORint 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 elementanArray[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
Why use Arrays?
5
Implementing List using Array the List interface is generic hence, the type parameter T in List<T>, 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<Double> 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
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
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
Constructors
public BoundedArrayList(int capacity) {
this.size = 0;
this.elements = new Object[capacity];
}
//default constructor, delegating constructor above
public BoundedArrayList() {
this(BoundedArrayList.CAPACITY);
}
9
The Copy Constructorwe 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
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
getSize Methodpublic int getSize()
{
return this.size;
}
12
get Methodget(index) returns the element of the list with the given indexthe 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
add Methodpublic 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
contains Methodpublic 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
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
remove Method
loop invariant for the second loop∀if < j < i : this.elements[j] has been copied
to this.elements[j − 1]
17
equals Method
loop invariant for the second loopequal == this.size() == other.size() &&
0 ∀ j < i : this.elements[j].equals(other.elements[j])
18
hashCode Methodpublic 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 31j ×
this.elements[j].hashCode()
19
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
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
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
toString Methodpublic 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
iterator Methodpublic Iterator<T> iterator()
{
return new BoundedArrayListIterator<T>(this.elements);
}
remember, the iterator method in the Iterable interface returns an object of type IteratorIterator 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
/*
* class invariant: this.next >= 0 &&
* for all 0 <= i < this.elements.length : this.elements[i] instanceof T
*/
25
Unbounded Sizepublic 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