710 likes | 870 Views
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
E N D
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 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
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
複製 (clone)obj1得到obj2 A obj2 =(A) obj1.clone() ; obj1 obj2 10 100 hello helloworld Yes No
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 個元素 }}
// obj1before clone ( ) is: 10 hello Yes // obj1after clone ( ) is: 10 hello No // 因 obj1 & obj2 refer 同一ArrayList obj2 複製 obj1
Fields in ArrayList class private transient E[ ] elementData; // “transient” 指如這ArrayList object 被serialized, // array elementData不被儲存. //但個別的element會被儲存 private int size;
// ArrayList初始化 ArrayList object為具有 // 容量initialCapacity的空List. public ArrayList (int initialCapacity) {elementData = (E[ ]) new Object [initialCapacity];} // 有一個parameter 的constructor
// ArrayList初始化 ArrayList object為具有 // 固定容量10的空List. public ArrayList ( ) {this (10);}
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
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
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:
1. Clone A1 A2 A3 2. Copy Constructor A1 A2 A3 null null null null null null null A1 A2 A3 nullnullnullnullnullnullnull
Iterators not needed for ArrayList for (int j = 0; j < myList.size( ); j++) System.out.println (myList.get (j));
But iterators are legal: Iterator<Double> itr = myList.iterator( ); while (itr.hasNext( )) System.out.println (itr.next( ));
Even better: for (Double d : myList) System.out.println (d);
本書有九個範例 (applications), 就像軟體博物館的九個著名作品, 你是初學者,站在名作面前, 要用心凝神揣摩 大師 精巧而流暢的創作手藝, 特別是 reuse high-level data structure 功力 才能學成技藝,成為達人! 下面是第一個範例: 軟體達人 學藝記
6.3 Application: High-Precision Arithmetic In public-key cryptography, the integers are hundreds of digits long.
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
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 的數目.