Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Type (how to view it)

Similar presentations


Presentation on theme: "Data Type (how to view it)"— Presentation transcript:

1 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

2 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

3 11/23/2018 IT 179 package myBigInteger;
public interface BigInt extends Comparable<BigInt>{ /** * Add a BigInt x into this BigInt. 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. x to be substracted public void subtract(BigInt x); * Time BigInt a with this BigInt and store back the result to this BigInt. 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. b is the divisor. r is a place for remainder. public void div(BigInt b, BigInt r); } 11/23/2018 IT 179

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

5 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

6 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

7 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. 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

8 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

9 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

10 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

11 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

12 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

13 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

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

15 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

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


Download ppt "Data Type (how to view it)"

Similar presentations


Ads by Google