IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.

Slides:



Advertisements
Similar presentations
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Advertisements

CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Arrays An array is a collection of variables, all of the same type. An array is created with the new operator. The size of an array must be specified at.
15-Jun-15 Lists in Java Part of the Collections Framework.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
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.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
The Java Collections Framework (JCF) Introduction and review 1.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Peter Andreae Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Implementing.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
Chapter 5: The List Abstract Data Type
JAVA COLLECTIONS LIBRARY
Implementing ArrayList Part 1
Programming in Java Lecture 11: ArrayList
CSE 143 Lecture 27: Advanced List Implementation
Collections in Java The objectives of this lecture are:
ArrayLists 22-Feb-19.
Collections Framework
ArrayList.
Web Design & Development Lecture 6
Part of the Collections Framework
CSE 143 Lecture 21 Advanced List Implementation
Arrays and ArrayLists.
Java Generics & Iterators
Presentation transcript:

IMPLEMENTING ARRAYLIST COMP 103

RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are for 2 nd year…  Implementing the ArrayList! 2 RECAP-TODAY

Interfaces and Classes  Interface  Specifies type  defines method headers only  Class  implements Interface  defines fields for the data, structures to hold the data.  defines method bodies  defines constructors  List ‣ Specifies sequence of E ‣ size, add 1, get, set, remove 1 ‣ add 2, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, indexOf, lastIndexOf, iterator, listIterator, remove 2, removeAll,retainAll, subList, toArray.  ArrayList ‣ implements List ‣ defines array of ‣ defines size, add, … ‣ defines constructors 3

List “extends” Collection  List has all the methods specified in the Collection interface, plus a few more.  List interface inherits all of the methods specified in Collection interface 4

inheritance ("extends") public class Dictionary extends Book {... } Java uses the reserved word extends to indicate that a new class (or interface) is being derived from an existing one  All the methods defined in the parent class, and all its variables (if any) are inherited. Extends establishes an "is-a" relationship.  every List is a Collection  every Dictionary is a Book But SWEN221 is all about this stuff – you only need to get the basic idea at this point 5

“extends” vs “implements”  public class Dictionary extends Book {... }  every Dictionary is a Book  public interface List extends Collection {... }  every List is a Collection compare this with implements:  some class or interface implements some interface  It is still a is a relationship: ArrayList is a List  But in Java can extend only one other class (or interface in case of declaring an interface) but can implement many interfaces eg:  public class ArrayList implements List {... }  public abstract class AbstractList implements List {... } 6

Defining ArrayList  Design the data structures to store the values  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) addAll(Collection c)addAll(int index, Collection c) containsAll(Collection c)removeAll(Collection c)toArray() subList(int fromIndex, int toIndex) retainAll(Collection c)toArray(T[] a) 7

Defining ArrayList: too many methods!  Problem: There are a lot of methods in List that need to be defined in ArrayList, and many are complicated.  But, many could be defined in terms of a few basic methods: (size, add, get, set, remove)  for example, public boolean addAll (Collection other) { for (E item : other) add(item); : }  Solution: an Abstract class  Defines the complex methods in terms of the basic methods  Leaves the basic methods “abstract” (no body)  classes implementing List can extend the abstract class. 8

Interfaces and Classes  Interface  Specifies type  defines method headers  Abstract Class  implements Interface  defines some methods  leaves basic methods “abstract”  Class  extends Abstract Class  defines data structures  defines basic methods  defines constructors  List ‣ Specifies sequence of etype  AbstractList ‣ implements List ‣ defines addAll, subList, … ‣ add, set, get, … left abstract  ArrayList ‣ extends AbstractList ‣ defines array of ‣ implements fields & constructor ‣ implements add, get, … 9

AbstractList public abstract class AbstractList implements List { public abstract int size(); public boolean isEmpty() { return (size() == 0); } public abstract void add(int index, E element); public abstract E get(int index); public boolean add(E element) { add(size(), element); declared abstract - must be defined in a real class defined in terms of size() declared abstract - must be defined in a real class defined in terms of other add 10 declared abstract - must be defined in a real class

AbstractList continued public boolean contains(Object ob){ for (int i = 0; i<size(); i++) if (get(i).equals(ob) ) return true; return false; } public void clear(){ while (size() > 0) remove(0); } :  Note: AbstractList cannot be instantiated because some methods are not implemented… defined in terms of size and get defined in terms of size and remove 11

ArrayList extends AbstractList public class ArrayList extends AbstractList { // fields to hold the data: need an array for the items and an int for the count. // constructor(s) Initialise the array // definitions of basic methods not defined in AbstractList size() get(index) set(index, value) remove(index) add(index, value) iterator() // (other methods are inherited from AbstractList ) // definition of the Iterator class 12

Implementing ArrayList  Data structure: data count  size:  returns the value of count  get and set:  check if within bounds, and  access the appropriate value in the array  add (index, elem):  check if within bounds, (0..size)  move other items up, and insert  as long as there is room in the array !!! 13

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]; }  Can’t use type variables as array constructors!!!! (Has to do with the type of elements being checked at run time as opposed to compile time for normal types – see later SWEN courses.)  Must Create as Object[ ] and cast to E[ ]  The compiler will complain, but ignore it!  “ …. uses unchecked or unsafe operations” ( E[ ]) new Object[INITIALCAPACITY]; 14

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

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

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

ArrayList: remove 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 ans = data[index];←remember for (int i=index; i < count; i++)←move items down data[i]=data[i+1]; count--; ←decrement return ans; ←return } 9 What’s the problem? 18

ArrayList: remove (fixed) 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 ans = data[index];←remember for (int i=index+1; i < count; i++)←move items down data[i-1] = data[i]; count--; ←decrement data[count] = null;←delete reference return ans; ←return } 9 19