Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing ArrayList Part 1 2015-T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,

Similar presentations


Presentation on theme: "Implementing ArrayList Part 1 2015-T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,"— Presentation transcript:

1 Implementing ArrayList Part 1 2015-T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean, Rashina Hoda, and Peter Andreae COMP 103 Thomas Kuehne

2 RECAP In the last lectures we looked at  Lists, Queues, Stacks  Sets, Maps TODAY We will implement ArrayList from scratch to  gain a deeper understanding of ArrayList  make a first foray into complexity analysis  learn how to implement a Java Collections Library interface 2 RECAP-TODAY

3 extends implements “extends” implies subtyping; instances of subtypes will behave like their supertypes expect them to “implements” signifies realisation; classes promise to fulfill the contracts defined by (interface) types

4 Inheritance ("extends") public class Square extends Shape {...} public class Dictionary extends Book {... } public interface List extends Collection {... } Java uses the reserved word extends to indicate that a class or interface is being derived from an existing one. Extends establishes a subtype relationship.  every Dictionary is a (behaves like a) Book  every List is a (behaves like a) Collection To support this, all methods defined in the parent class (and all its variables, if any) are inherited. More on this in SWEN221… 4

5 Interfaces and Classes  Interface  Specifies type  (typically) defines method signatures only  Class  Implements interface  defines variables for the data  defines method bodies  defines constructors  List ‣ Specifies sequence of E ‣ size, add, get, set, remove, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, indexOf, lastIndexOf, iterator, listIterator, …  ArrayList ‣ implements List ‣ defines array of ‣ defines size, add, … ‣ defines constructors 5

6 Defining ArrayList  Design the data structures to store the elements  array of items  count  Define the fields and constructors public class ArrayList implements List { private E [ ] data; private int count; …  Define all the methods specified in the List interface: size()add(E o)add(int index, E element)contains(Object o)get(int index) isEmpty()clear()set(int index, E element)indexOf(Object o)remove(int index) remove(Object o)lastIndexOf(Object o)iterator() equals(Object o) hashCode()listIterator() listIterator(int index) … 6

7 Implementing ArrayList  Data structure: data count  size()  returns the value of count  get(index) and set(index, elem)  check if index is within bounds, and  access/set the appropriate slot in the array  add (index, elem)  check if index is within bounds, (0..size)  (move other items up, and) insert element  ensure further capacity if the currently used array becomes too small 7

8 We could do this, but it would imply a lot of work! ArrayList: fields and constructor public class ArrayList implements List { private E[ ] data; private int count=0; private static final int INITIALCAPACITY = 16;. 8

9 ArrayList: fields and constructor public class ArrayList extends AbstractList { private E[ ] data; private int count=0; private static final int INITIALCAPACITY = 16; public ArrayList() { data = new E [INITIALCAPACITY]; }  We cannot use type variables as array constructors!  Have to create as Object[ ], and then cast to E[ ]  Compiler will warn, but the warning is safe to ignore here  may use “@SuppressWarnings("unchecked")” 9 ( E[ ]) new Object[INITIALCAPACITY]; This saves us considerable effort and will be explained in the next lecture

10 ArrayList: size(), isEmpty() public class ArrayList extends AbstractList { private E[ ] data; private int count=0; : /** @return number of elements in collection as integer */ public int size () { return count; } /** @return true, if this set contains no elements. */ public boolean isEmpty() { return count==0; } 9 10

11 ArrayList: get(index) public class ArrayList extends AbstractList { private E[ ] data; private int count=0; : /** @return the element at the specified index. * Throws an IndexOutOfBoundsException, if index is out of bounds*/ public E get(int index) { if (index = count) throw new IndexOutOfBoundsException(); return data[index]; } 9 11

12 ArrayList: set(index, element) public class ArrayList extends AbstractList { private E[ ] data; private int count=0; : /** Replaces the element at the specified index by the specified element. * @return the old element. * Throws an IndexOutOfBoundsException, if index is out of bounds */ public E set(int index, E element) { if (index = count) throw new IndexOutOfBoundsException(); E oldElement = data[index]; data[index] = element; return oldElement; } 9 12

13 ArrayList: remove(index) public class ArrayList extends AbstractList { private E[] data; private int count=0; /** Removes the element at the specified index, and returns it. * Throws an IndexOutOfBoundsException if index is out of bounds */ public E remove (int index) { if (index = count) throw new IndexOutOfBoundsException(); E oldElement = data[index];← remember old element for (int i=index; i < count-1; i++) ← move items down data[i]=data[i+1]; count--; ← adjust size variable data[count] = null;← delete reference return oldElement; (  garbage collection) } 9 13

14 ArrayList: add (pseudocode) public class ArrayList extends AbstractList { private E[] data; private int count=0; : // Adds the specified element at the specified index public void add(int index, E item) { // check that the index is within the allowed bounds // above the index, move the items up, starting at the last // insert the item into the gap // adjust the size variable } 9 14

15 ArrayList: add public class ArrayList extends AbstractList { private E[] data; private int count=0; : // Adds the specified element at the specified index public void add(int index, E item) { if (index < 0 || index count) throw new IndexOutOfBoundsException(); for (int i=count; i > index; i--) ← move items up data[i] = data[i-1]; data[index] = item; ← insert element count++; ← adjust size variable }  What’s wrong ??? (two issues) 9 15 ← can add at end! ensureCapacity(); ← make room, if required >= >

16 Ensuring Capacity  Three steps: 8 data count newArray data.length = 8 16 ArrayList

17 ArrayList: ensureCapacity /** * Ensures data array has sufficient number of elements * to add a new element */ private void ensureCapacity () { if (count < data.length) return; ← there is room already E [ ] newArray = (E[ ]) (new Object[data.length + …....] ); for (int i = 0; i < count; i++) ← copy to the new array newArray[i] = data[i]; data = newArray; ← replace old array with new one } 17 How much bigger should this new array be? One ? INITIALCAPACITY ? data.length ?


Download ppt "Implementing ArrayList Part 1 2015-T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,"

Similar presentations


Ads by Google