1 / 36

Comp 110/401 Collection Kinds

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.

brigit
Download Presentation

Comp 110/401 Collection Kinds

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comp 110/401Collection Kinds Instructor: PrasunDewan

  2. Prerequisite • Arrays Collections Implementation

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

  4. History • public interface StringHistory { • publicvoidaddElement(String element); • publicint size(); • public String elementAt(int index); • }

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

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

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

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

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

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

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

  12. 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); }

  13. 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); • … • }

  14. 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); • … • }

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

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

  17. APointHistory Variable-sized Collection History Methods added to menu associated with class Graphic elements of dynamic collections added at their (X, Y) locations

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

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

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

  21. 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);

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

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

  24. 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(); • }

  25. Tables Each element identified by a unique object called a key Usually strings are used as keys

  26. 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);

  27. 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); }

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

  29. Extra Slides

  30. indexOf(String element) indexOf(“James Dean”); size array 4 James Dean Joe Doe index Jane Smith Jane Smith 0 John Smith

  31. 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(); }

  32. StringDatabase Commands

  33. Generic List: Vector java.util.List strings = newjava.util.Vector(); James Dean strings.add“JamesDean”) Joe Doe strings.add(“Joe Doe”)

  34. 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”)

  35. 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() {…}; • }

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

More Related