1 / 71

Chapter 4

Chapter 4. The Java Collections Framework. 4.-1 Overview 4.0 Discrete Math Models 4.1 Collections 4.1.1 Collection Classes 4.1.2 Storage Structures for Collection Classes 4.2 Outline of the Java Collections Framework 4.2.1 Abstract Classes 4.2.2 Parameterized types

marenm
Download Presentation

Chapter 4

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. Chapter 4 The Java Collections Framework

  2. 4.-1 Overview 4.0 Discrete Math Models 4.1 Collections 4.1.1 Collection Classes 4.1.2 Storage Structures for Collection Classes 4.2 Outline of the Java Collections Framework 4.2.1 Abstract Classes 4.2.2 Parameterized types 4.2.3 The Collection Interface 4.2.4 The List Interface 4.2.5 The Set Interface 4.2.6 The Map Interface Chap.4 Contents

  3. 4.-1 Overview • A collection is an object that is composed of elements. • The elements can be either references to object (such as String) OR primitive values (such as int) String[ ] names = new String[5]; int[ ] numbers = new int[5]; names ref string1 numbers int string2

  4. A collection class is a class in which each instance (object) is a collection of elements, and each element is a reference to an object. EX: ArrayList<String> names = new ArrayList<String>(5); Parameterized class specifies element type in angle bracket < >. EX: ArrayList<Integer> numbers = new ArrayList<Integer>(5); Conversion between primitive data and object: int i; Integer myInt; String s; myInt = i; i = myInt; s = myInt.toString();

  5. Interface(class interface) interface I1{ String m1(); int m2(); } abstract class abstract class A1 implement I1{ int i; String m1(){return “m1”;} int m2 (); } Class class C1 extends A1{ int j; int m2(){return “”c1} } class C2 extends A1{ int k; int m2(){return “c2”} }

  6. A design patternis a problem (ex: collection iteration) that occurs frequently and the outline of a solution (ex: the iterator interface below). interface Iterator<Entry> { boolean hasNext(); Entry next(); void remove(); }

  7. The three models will be covered: 1. Set 2. Map 3. List (Sequence) 4.0 Discrete Math Models

  8. Ex. { a, b, c} A set is an unordered collection of objects, called elements or members of the set. Set

  9. Ex: { a->1, b->2, c->2 } A map is a set of function mappings, each maps an element (called key) in a set (called domain) to an element (called value) in a set (called codomain). Map

  10. Ex. < a0, a1, a2> A list (sequence) is a function from a subset of the set of integers, usually {0,1,2,…} (called “index”) to a set S. an denotes the function mapping of the integer n. List

  11. This course will use various data structures to implement the discrete models mentioned above. For example, “ArrayList” class uses array to implement list, while “LinkedList” class uses linked structure to implement the same list. Discrete Structure (Discrete Math and data structure)

  12. 4.1 Collections

  13. Example

  14. To small? A larger array must be allocated and the contents of the smaller array copied to the larger array. Example: double[ ] salaries = new double [1000]; if salaries is too small, First double [ ] newSalaries = new double [2000]; Then System.arraycopy (salaries, 0, newSalaries, 0, 1000); Finally salaries = newSalaries;

  15. 假設 array salaries 有100個元素空間,現已佔用了位置0~79. 欲將28000.00 寫入index 41. 則原本在salaries[41]的元素 移動到salaries[42],後面依此類推 Example

  16. Better than arrays: Instances of collection classes A collection classis a class whose instances are collections. 4.1.1 Collection Classes

  17. collection 中的元素必須為(references to) objects. Example: a String object can be an element, 基本型別 (int, double, boolean, …) 不能作為 collection 的元素, 但是對於每種基本型別都有其對應的 class (叫 wrapper classes )可用 如: Integer, Double, Boolean, …

  18. 4.1.2 Storage Structures for Collection Classes E for element Another hierarchy Map is not shown here

  19. Example Entry object

  20. 4.2 Outline of the Java Collections Framework

  21. 4.2.1 Abstract Classes

  22. A few more details on the relationship between: interfaces, abstract classes, and fully defined classes:

  23. If a class implements some but not all of the methods in an interface, then the class would have to be declared as an abstract class ─ and therefore cannot be instantiated. An interface can extend one or more other interfaces. Ex: public interface A extends B, C{ …. A class can extend at most one other class; by default, the Object class is the superclass of every class. Multiple inheritance is illegal in Java. A class can implement more than one interface. Ex: class D implements interface1, interface2{ ….

  24. Starting with J2SE (Java 2 Platform Standard Edition) version 1.5, a class’s element type is specified in angle brackets (< >), when an instance of the class is declared. 4.2.2 Parameterized types

  25. /*boxing : 將 ”值” 包裝成 ”物件” */

  26. Method get() Returns a Double object

  27. 4.2.3 The Collection Interface

More Related