Data Type (how to view it)

Slides:



Advertisements
Similar presentations
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Advertisements

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)
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
ArrayList, Multidimensional Arrays
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()
Information and Computer Sciences University of Hawaii, Manoa
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
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.
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
Single-Linked Lists.
The List ADT Reading: Textbook Sections 3.1 – 3.5
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
ADT’s, Collections/Generics and Iterators
ARRAYLIST AND VECTOR.
COP 3503 FALL 2012 Shayan Javed Lecture 8
A tree set Our SearchTree class is essentially a set.
Implementing ArrayList Part 1
Java collections library
List Representation - Array
Top Ten Words that Almost Rhyme with “Peas”
Welcome to CSE 143!.
Building Java Programs
MyList<T> It’s a generic type, <T> is a type parameter
TCSS 143, Autumn 2004 Lecture Notes
Priority Queues.
Lecture 2: Implementing ArrayIntList reading:
Programming in Java Lecture 11: ArrayList
Building Java Programs
Data Structures ADT List
Data Structures ADT List
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Building Java Programs
The List ADT Reading: Textbook Sections 3.1 – 3.5
(Java Collections Framework) AbstractSequentialList
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
ArrayList Collections.
Welcome to CSE 143!.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Lecture 2: Implementing ArrayIntList reading:
Dynamic Data Structures and Generics
Chapter 6 Array-Based Lists.
Computer Science and Engineering
ArrayLists 22-Feb-19.
Collections Framework
Binary Search Trees continued; Tree Sets
Data Structures ADT List
slides created by Marty Stepp
Review of Previous Lesson
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists 27-Apr-19.
Programming II (CS300) Chapter 07: Linked Lists
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
CSE 143 Lecture 21 Advanced List Implementation
Copyright ©2012 by Pearson Education, Inc. All rights reserved
What can you put into an ArrayList?
Presentation transcript:

Data Type (how to view it) Specification View of a data type Data values Operations defined on those values Abstract Data Type (ADT) –use of abstraction! Implementation View of a data type Language-specific representation for the values Implementation of the operations  Implementation of an ADT 11/23/2018 IT 179

Inherit classes & Implement Interface Object + toString(): String + equals(o: Object): boolean Comparable<T> {interface} + compareTo(o:T): int implicit BigInt {interface} + add(x:BigInt): void + substract(x:BigInt): void + times (x:BigInt): void + div(x:BigInt, r:BitInt): void MyBigInt + add(a:MyBigInt, b:MyBigInt): MyBigInt ...... 11/23/2018 IT 179

11/23/2018 IT 179 package myBigInteger; public interface BigInt extends Comparable<BigInt>{ /** * Add a BigInt x into this BigInt. * @param x to be added */ public void add(BigInt x); * Subtract a BigInt x from this BigInt. * If x is bigger than this, then the result is zero. * @param x to be substracted public void subtract(BigInt x); * Time BigInt a with this BigInt and store back the result to this BigInt. * @param a is the multiplier public void times(BigInt a); * Divide this BigInt by BigInt b and store back the result to this BigInt * and store the remainder in r. * @param b is the divisor. * @param r is a place for remainder. public void div(BigInt b, BigInt r); } 11/23/2018 IT 179

BigInt implementation For Honor Project 11/23/2018 IT 179

Inside the ArrayList<T> [0] public int size(); public T get(int i); public T set(int i, T item); public int indexOf(T item); public void add(T item); public void add(int i, T item); public T remove(int i); public Object[] toArray(); public <K> K[] toArray(K[] a); [1] B [2] C [3] D [4] E [5] F [6] G [7] H [8] [9] 11/23/2018 IT 179

Two types of arrays Static array Dynamic array (JAVA’s type, objects) size determined at compile time can’t change afterwards Dynamic array (JAVA’s type, objects) Size determine at run time Can be resized (this is somehow an overstatement) 11/23/2018 IT 179

RandomAccess, Serialization, Cloneable { private int size; public class ArrayList<T> implements Iterable<T>, Collection<T>, List<T>, RandomAccess, Serialization, Cloneable { private int size; private T[] data; /** * This constructor will set the internal array to an Object array of size 10. */ public ArrayList() { data = (T[]) new Object[10]; size=0; } * This constructor will set the internal array to an Object array of size specified * by the user using parameter capacity. * @param capacity is the initial size of the internal array public ArrayList(int capacity) { data = (T[]) new Object[capacity]; this.size=0; 11/23/2018 IT 179

public int indexOf(Object item) { for (int i=0; i<size; i++) if (data[i].equals(item)) return i; return -1; } public Object[] toArray() { Object[] t = new Object[size]; for (int i=0; i< size; i++) t[i] = data[i]; return t; } 11/23/2018 IT 179

Add an element – O(n) Remove an element – O(n) 4 5 7 8 9 10 11 4 5 7 8 9 10 11 4 5 7 8 9 10 11 Delete 7 6 6 ? Resizing operation is needed 11/23/2018 IT 179

public void add(int at, T item) { // add item to position i if (at < 0 || at > size) throw new ArrayIndexOutOfBoundsException(i); if (size == data.length) // the Data array is full doubleCapacity(); for (int i=size;i>at;i--) { data[i] = data[i-1]; } size++; data[at] = item; public T remove(int i) { if (at < 0 || at >= size) throw new ArrayIndexOutOfBoundsException(i); T item = data[at]; for (int i = at; i<size-1; i++) data[i] = data[i+1]; size--; return item; } 11/23/2018 IT 179

Speedup the three slow operations on Array Resizing, Insertion, Deletion Size of the array Capacity of the array Java provides a class called ArrayList that implements this idea 11/23/2018 IT 179

ArrayList () Constructs an empty list with an initial capacity of ten. Constructor Summary ArrayList ()           Constructs an empty list with an initial capacity of ten. ArrayList (int  initialCapacity)           Constructs an empty list with the specified initial capacity. import java.util.ArrayList; ..... ArrayList<String> L = new ArrayList<String>(); ArrayList<Robot> r = new ArrayList<Robot>(100); ArrayList<Student> it179 = new ArrayList<Student>(50); 11/23/2018 IT 179

Resizing – O(size of new array) operation 4  8 But, why not O(size of old array)? This may not be movable or difficult to move 11/23/2018 IT 179

Resizing – O(size of new array) operation 4  8 11/23/2018 IT 179

private void doubleCapacity() { int c = data.length*2; T[] newList = (T[]) new Object[c]; for (int i =0; i< data.length; i++) newList[i] = data[i]; // This is OK? data = newList; } 11/23/2018 IT 179

Using an inner class for nodes public interface SimpleList <T extends Comparable<T> > { ..... public T min(); public T max(); } 11/23/2018 IT 179