360 likes | 461 Vues
Comp 110/401 Collection Kinds. Instructor: Prasun Dewan. Prerequisite. Arrays Collections Implementation. Static vs. Dynamic Structures. Static. Beans have fixed number of properties. Arrays have fixed number of elements. Though an array variable can be assigned arrays of different sizes.
E N D
Comp 110/401Collection Kinds Instructor: PrasunDewan
Prerequisite • Arrays Collections Implementation
Static vs. Dynamic Structures Static Beans have fixed number of properties Arrays have fixed number of elements Though an array variable can be assigned arrays of different sizes
History • public interface StringHistory { • publicvoidaddElement(String element); • publicint size(); • public String elementAt(int index); • }
Database public interface StringDatabase { //from history publicint size(); publicvoidaddElement(String element); public String elementAt(int index); //additional methods publicvoidremoveElement(String element); publicbooleanmember(String element); publicvoid clear(); } Do we need a history if we have a database? Yes, principle of least privilege
Principle of Least Privilege • Do not give a user of some code more rights than it needs • Code is easier to change • Need to learn less to use code • Less likelihood of accidental or malicious damage to program • Like hiding engine details from car driver
Using Database as History public interface StringDatabase { //from history publicint size(); publicvoidaddElement(String element); public String elementAt(int index); //additional methods publicvoidremoveElement(String element); publicbooleanmember(String element); publicvoid clear(); } Programmer would be able to perform inappropriate operations on a logical history implemented physically as a database
Co-Existence public interface StringDatabaseextendsStringHistory { //additional methods publicvoidremoveElement(String element); publicbooleanmember(String element); publicvoid clear(); } Programmer would be able to perform inappropriate operations on a logical history implemented physically as a database
Vector: General Object Collection publicfinalint size(); publicfinal Object elementAt(int index); publicfinalvoidaddElement(Object obj) ; publicfinalvoidsetElementAt(Object obj, int index); publicfinalvoidinsertElementAt(Object obj, int index); publicfinalbooleanremoveElement(Object obj); publicfinalvoidremoveElementAt(int index); publicfinalintindexOf(Object obj); … Do we need other collections if we have Vector Yes, principle of least privilege Yes, implementation considerations
Class ArrayList And Vector (List) publicfinalint size(); publicfinal Object get(intindex); publicfinalvoidadd(Object obj) ; publicfinalvoidset(int index, Object obj); publicfinalvoidinsert(int index, Object obj); publicfinalbooleanremove(Object obj); publicfinalvoidremove(intindex); publicfinalintindexOf(Object obj); … Vector has ArrayList (List) methods plus the additional original methods in the previous slides Can add arbitrary objects to these collections
Array List and Vector User importjava.util.ArrayList; importjava.util.List; importjava.util.Vector; publicclassVectorArrayListUser { publicstaticvoid main (String[] args) { List names = new Vector(); List grandSlams = newArrayList(); names.add("Nadal"); grandSlams.add(11); names.add("Federer"); grandSlams.add(17); names.add(“Borg"); grandSlams.add(11); names.add(“Sampras"); grandSlams.add(14); } } Primitive values converted to corresponding wrapper objects
Visualizing Collections • public interface StringHistory { • publicvoidaddElement(String element); • publicint size(); • public String elementAt(int index); • } publicstaticvoid main (String[] args) { StringHistorystringHistory = newAStringHistory(); stringHistory.addElement("James Dean"); stringHistory.addElement("Joe Doe"); stringHistory.addElement("Jane Smith"); stringHistory.addElement("John Smith"); ObjectEditor.edit(stringHistory); }
Read methods Arbitrary Type. Unconstrained Type (void or T in practice) Conventions for Variable-Sized Collection Write method (optional) Convention based on Vector • @StructurePattern(StructurePatternNames.VECTOR_PATTERN) • publicinterface C{ • publicT elementAt (int index); • publicint size(); • publicAny setElementAt(T t, int index); • … • }
Read methods Arbitrary Type. Unconstrained Type (void or T in practice) Alternative Conventions for Variable-Sized Collection Write method (optional) Convention based on ArrayList • @StructurePattern(StructurePatternNames.LIST_PATTERN) • publicinterfaceC { • public T get (int index); • publicint size(); • publicAny set (int index) T 2); • … • }
Read vs. Write Methods • Read Methods • Used to get components of object • Getter methods • size(), elementAt() • Write Methods • Used to change components of object • Setter methods • addElement(), removeElement(), setElementAt() • some used by Object Editor • Distinction independent of conventions • Conventions used in Object Editor
Conventions for Variable-Sized Collection Write Method not recognized by OE • publicinterfacePointHistory { • publicvoidaddElement (int x, int y); • public Point elementAt (int index); • publicint size(); • } Read Methods Arbitrary Type
APointHistory Variable-sized Collection History Methods added to menu associated with class Graphic elements of dynamic collections added at their (X, Y) locations
Important Variable Sized Collections • Variable-sized collections • History • Orders elements • Allows retrieval, addition but not replacement or deletion • Point History, String History • Database • May order elements • Allows retrieval, search, addition, insertion, and unconstrained removal • StringDatabase (did not but should have supported insertion) • Sets • No duplicates Other collection kinds?
Stack: Last in First Out • publicinterfaceStringStack { • publicbooleanisEmpty(); • public String getTop(); • publicvoid push(String element); • publicvoid pop(); • } • publicinterfaceStringStack { • publicbooleanisEmpty(); • public String getTop(); • publicvoid push(String element); • publicvoid pop(); • } StringStackstringStack = newAStringStack(); stringStack.push("James Dean"); stringStack.push("Joe Doe"); stringStack.push("Jane Smith"); stringStack.push("John Smith"); System.out.println(stringStack.getTop()); stringStack.pop(); System.out.println(stringStack.getTop()); John Smith Jane Smith
Queue: First in First Out • publicinterfaceStringStack { • publicbooleanisEmpty(); • public String getTop(); • publicvoid push(String element); • publicvoid pop(); • } • publicinterfaceStringQueue { • publicbooleanisEmpty(); • public String getHead(); • publicvoidenqueue(String element); • publicvoid dequeue(); • } StringQueuestringQ = newAStringQueue(); stringQ.enqueue("James Dean"); stringQ.enqueue("Joe Doe"); stringQ.enqueue("Jane Smith"); stringQ.enqueue("John Smith"); System.out.println(stringStack.getHead()); stringQ.dequeue(); System.out.println(stringStack.getHead()); James Dean Joe Doe
Displaying Stack (LIFO) • publicinterfaceStringStack { • publicbooleanisEmpty(); • public String getTop(); • publicvoid push(String element); • publicvoid pop(); • } Does not provide read methods for reading all elements StringStackstringStack = newAStringStack(); stringStack.push("James Dean"); stringStack.push("Joe Doe"); stringStack.push("Jane Smith"); stringStack.push("John Smith"); ObjectEditor.edit(stringStack);
Displaying Transparent Stack (LIFO) • publicinterfaceTransparentStringStack { • publicint size(); • public String get(int index); • publicvoid push(String element); • publicvoid pop(); • } Provides read methods following OE collection conventions StringStackstringStack = newAStringStack(); stringStack.push("James Dean"); stringStack.push("Joe Doe"); stringStack.push("Jane Smith"); stringStack.push("John Smith"); bus.uigen.ObjectEditor.edit(stringStack); Can provide additional method for top value
Array List and Vector User importjava.util.ArrayList; importjava.util.List; importjava.util.Vector; publicclassVectorArrayListUser { publicstaticvoid main (String[] args) { List names = new Vector(); List grandSlams = newArrayList(); names.add("Nadal"); grandSlams.add(11); names.add("Federer"); grandSlams.add(17); names.add("Djokovic"); grandSlams.add(5); names.add(“Borg"); grandSlams.add(11); } } What kind of dynamic structure is being simulated?
Indexed Collections Each element identified by a unique index Like array indices, collection indices are consecutive Highest index – Lowest Index = size -1 • publicinterfaceTransparentStringStack { • publicint size(); • public String get(int index); • publicvoid push(String element); • publicvoid pop(); • }
Tables Each element identified by a unique object called a key Usually strings are used as keys
HashMap (Implement Map) // associates key with value, returning last value associated with key publicfinal Object put (Object key, Object value); // returns last value associated with key, or null if no association publicfinal Object get (Object key);
HashMap Use publicstaticvoid main (String[] args) { Map aMap = newHashMap(); aMap.put("Nadal", 10); aMap.put("Federer", 17); aMap.put("Sampras", 14); System.out.println(aMap.get("Nadal")); System.out.println(aMap.get("nadal")); aMap.put("Nadal", 11); System.out.println(aMap.get("Nadal")); ObjectEditor.edit(aMap); }
OE Conventions For Table // associates key with value, returning last value associated with key public <ValueType> put (<KeyType> key, <ValueType> value); // returns last value associated with key, or null if no association public <ValueType> get (<KeyType> key); // optional, removes associated value, and returns it or null public <ValueType> remove(<KeyType> key); Necessary but not sufficient to displays all keys and elements
indexOf(String element) indexOf(“James Dean”); size array 4 James Dean Joe Doe index Jane Smith Jane Smith 0 John Smith
Visualization of Variable-Sized Collections • public interface StringHistory { • publicvoidaddElement(String element); • publicint size(); • public String elementAt(int index); • } public interface StringDatabase { //from history publicint size(); publicvoidaddElement(String element); public String elementAt(int index); //additional methods publicvoidremoveElement(String element); publicbooleanmember(String element); publicvoid clear(); }
Generic List: Vector java.util.List strings = newjava.util.Vector(); James Dean strings.add“JamesDean”) Joe Doe strings.add(“Joe Doe”)
ArrayList (java.util.ArrayList) and List (java.util.List) java.util.List strings = newjava.util.ArrayList(); James Dean strings.add(“James Dean”) Joe Doe strings.add(“Joe Doe”)
Visualization of Variable-Sized Collections • public classAStringDatabaseimplementsStringHistory { • publicfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • booleanisFull() { return size == MAX_SIZE; } • publicvoidaddElement(String element) { …} • publicvoidremoveElement(String element) {…}; • publicbooleanmember(String element) {…}; • publicvoid clear() {…}; • }
Important Variable Sized Collections • Variable-sized collections • History • Orders elements • Allows retrieval, addition but not replacement or deletion • Point History, String History • Database • May order elements • Allows retrieval, search, addition, insertion, and unconstrained removal • StringDatabase (did not but should have supported insertion)