1 / 71

Chapter 6

Chapter 6. Array-Based Lists. 6.1 The List Interface 6.2 The ArrayList Class 6.2.1 Method Specifications for the ArrayList Class 6.2.2 A Simple Program with an ArrayList Object 6.2.3 The ArrayList Class’s Heading and Fields

Download Presentation

Chapter 6

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 6 Array-Based Lists

  2. 6.1 The List Interface 6.2 The ArrayList Class 6.2.1 Method Specifications for the ArrayList Class 6.2.2 A Simple Program with an ArrayList Object 6.2.3 The ArrayList Class’s Heading and Fields 6.2.4 Definition of the One-Parameter add Method 6.3 Application: High-Precision Arithmetic 6.3.1 Method Specifications of the VeryLongInt Class 6.3.2 Fields in the VeryLongInt Class 6.3.3 Method Definitions of the VeryLongInt Class Chap.6 Contents

  3. 6.1 The List Interface

  4. 6.2 The ArrayList Class

  5. 6.2.1 Method Specifications for the ArrayList Class

  6. 6.2.2 A Simple Program with an ArrayList Object

  7. 6.2.3 The ArrayList Class’s Heading and Fields

  8. class A { int a; String b; ArrayList c; public A( ){ a = 10; b = “hello”; c = new ArrayList(); c.add(“Yes”) ; } //end constructor } // end class A ArrayList CLONE Example

  9. 複製 (clone)obj1得到obj2 A obj2 =(A) obj1.clone() ;  obj1 obj2 10 100 hello helloworld Yes No

  10. class Main(){ // obj2 複製obj1     public static void main(String[] args){ A obj1 = new A() ;  // 印出 obj1 (Before Clone) System.out.println(obj1.a ) ;            System.out.println(obj1.b ) ;            System.out.println(obj1.c(0) ) ;  // 取ArrayList 第 0 個元素 /* obj2 複製obj1 */ A obj2 =(A) obj1.clone() ;  // 設定obj2 欄位 obj2.a=100;  obj2.b=“helloworld”; obj2.c.set(0, “No” ) ; //設定ArrayList 第 0 個元素為 “No” // 印出 obj1 ,看有否受 obj2影響(After Clone) System.out.println(obj1.a ) ;            System.out.println(obj1.b ) ;            System.out.println(obj1.c(0) ) ;  //取得ArrayList 第 0 個元素 }}

  11. // obj1before clone ( ) is: 10 hello Yes // obj1after clone ( ) is: 10 hello No // 因 obj1 & obj2 refer 同一ArrayList obj2 複製 obj1

  12. Fields in ArrayList class private transient E[ ] elementData; // “transient” 指如這ArrayList object 被serialized, // array elementData不被儲存. //但個別的element會被儲存 private int size;

  13. // ArrayList初始化 ArrayList object為具有 // 容量initialCapacity的空List. public ArrayList (int initialCapacity) {elementData = (E[ ]) new Object [initialCapacity];} // 有一個parameter 的constructor

  14. // ArrayList初始化 ArrayList object為具有 // 固定容量10的空List. public ArrayList ( ) {this (10);}

  15. 6.2.4 Definition of the One-Parameter add Method // add增加 element 到這一個 ArrayList object // 然後傳回 true. // AverageTime(n) is constant;worstTime(n) is O (n). // public boolean add (E element) { ensureCapacity (size + 1); elementData [size++] = element;return true; }//add

  16. public void ensureCapacity(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { {Increase the capacity by at least 50%, and copy the old array to the new array} }// ensureCapacity

  17. Here are two ways to create a copy ofmyList called newList: 這裡有2種產生myList 的copy (newList)的方式: 1. clone ArrayList<String> newList = (ArrayList<String>) myList.clone( ); 2. copy constructor ArrayList<String> newList = new ArrayList<String> (myList); Suppose myListhas 3 elements:

  18. 1. Clone A1 A2 A3 2. Copy Constructor A1 A2 A3 null null null null null null null A1 A2 A3 nullnullnullnullnullnullnull

  19. Iterators not needed for ArrayList for (int j = 0; j < myList.size( ); j++) System.out.println (myList.get (j));

  20. But iterators are legal: Iterator<Double> itr = myList.iterator( ); while (itr.hasNext( )) System.out.println (itr.next( ));

  21. Even better: for (Double d : myList) System.out.println (d);

  22. 本書有九個範例 (applications), 就像軟體博物館的九個著名作品, 你是初學者,站在名作面前, 要用心凝神揣摩 大師 精巧而流暢的創作手藝, 特別是 reuse high-level data structure 功力 才能學成技藝,成為達人! 下面是第一個範例: 軟體達人 學藝記

  23. 6.3 Application: High-Precision Arithmetic In public-key cryptography, the integers are hundreds of digits long.

  24. short 16 bits min -32,768 (2**15) max 32,767 (2**15 -1) int 32 bits min -2,147,483,648 max 2,147,483,647 long 64 bits min -9,223,372,036,854,775,808 max 9,223,372,036,854,775,807 Background on integers

  25. 6.3.1 Method Specifications of the VeryLongInt Class We will now develop a: VeryLongInt classto handle very long integers. In the method descriptions, n refers to the number of digits in the calling object. 我們將發展一個classVeryLongInt class來處理超長整數 在 method 描述, n 代表 calling object 中 digits 的數目.

More Related