【正文】
f numbers, Strings, (and ―parable‖ Objects). Methods: int pos = (arr, target)。 (arr)。 (arr, from, to)。 (arr, value)。 // fills arr with a given value (arr, from, to, value)。 交通大學(xué)資訊工程學(xué)系 蔡文能 第 46頁(yè) Java More Java Examples (1/2) Benchmarks uses the class — a more controlled way to generate random numbers. Constructors: If we set the same ―seed,‖ we get the same ―random‖ sequence. Random generator = new Random( )。 Random generator2 = new Random(seed)。 Default “seed”: () long seed。 交通大學(xué)資訊工程學(xué)系 蔡文能 第 47頁(yè) Java More Java Examples (2/2) Methods: int k = ()。 int k = (n)。 double x = ()。 All 232 possible int values are produced with (approximately) equal probability 0 ? k n 0 ? x 1 交通大學(xué)資訊工程學(xué)系 蔡文能 第 48頁(yè) Java More Java Examples Interface The List library interface describes a list of objects in abstract terms In a list, objects are arranged in sequence obj0, obj1, ..., objn1 In Java, a list holds references to objects A list can contain duplicate objects (both (obj2) and obj1 == obj2 ) 交通大學(xué)資訊工程學(xué)系 蔡文能 第 49頁(yè) Java More Java Examples List Methods (a Subset) int size()。 boolean isEmpty ()。 boolean add (Object obj)。 void add (int i, Object obj)。 Object set(int i, Object obj)。 Object get(int i)。 Object remove(int i)。 boolean contains(Object obj)。 int indexOf(Object obj)。 returns true inserts obj as the ith value。 i must be from 0 to size() i must be from 0 to size() 1 use equals to pare objects 交通大學(xué)資訊工程學(xué)系 蔡文能 第 50頁(yè) Java More Java Examples (1/6) Implements List using an array Keeps track of the list capacity (the length of the allocated array) and list size (the number of elements currently in the list) Cat Hat Bat capacity size 交通大學(xué)資訊工程學(xué)系 蔡文能 第 51頁(yè) Java More Java Examples ArrayList (2/6) Automatically increases (doubles) the capacity when the list runs out of space。 allocates a bigger array and copies all the values into it get(i) and set(i, obj) are efficient because an array provides random access to its elements Throws IndexOutOfBoundsException when i 0 or i ? size() 交通大學(xué)資訊工程學(xué)系 蔡文能 第 52頁(yè) Java More Java Examples ArrayList (3/6) ArrayList holds objects (of any type) If you need to put ints or doubles into a list, use a standard Java array or convert them into Integer or Double objects You have to remember what types of objects your put into the list and may need to cast a retrieved object back into its type 交通大學(xué)資訊工程學(xué)系 蔡文能 第 53頁(yè) Java More Java Examples ArrayList (4/6) From Java API Docs: : ArrayList(Collection? extends E c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection39。s iterator. 交通大學(xué)資訊工程學(xué)系 蔡文能 第 54頁(yè) Java More Java Examples ArrayList list = new ArrayList ()。 (new Integer(1))。 (new Integer(2))。 (new Integer(3))。 int sum = 0。 for (int i = 0。 i ()。 i++) sum += ((Integer) (i)) . intValue()。 ArrayList (5/6) Example 1 Need a cast to Integer in order to call intValue 交通大學(xué)資訊工程學(xué)系 蔡文能 第 55頁(yè) Java More Java Examples ArrayList words = new ArrayList (4)。 (One)。 (Fish)。 (Two)。 (Fish)。 int i = 0。 while (i () ) { if ( ”Fish.equals ((i)) ) (i)。 else i++。 } ArrayList (6/6) Example 2 Shifts all the values after the ith to the left and decrements the size 交通大學(xué)資訊工程學(xué)系 蔡文能 第 56頁(yè) Java More Java Examples Generic type 交通大學(xué)資訊工程學(xué)系 蔡文能 第 57頁(yè) Java More Java Examples Generic Programming Define software ponents with type parameters ? A sorting algorithm has the same structure, regardless of the types being sorted ? Stack primitives have the same semantics, regardless of the ? objects stored on the stack. Compile time parameters Implementation ? Same code used for different parameter values ? Different code generated for different values C++ model: template class Java : type inference 交通大學(xué)資訊工程學(xué)系 蔡文能 第 58頁(yè) Java More Java Examples More examples, … 交通大學(xué)資訊工程學(xué)系 蔡文能 第 59頁(yè) Java More Java Examples 謝謝觀看 /歡迎下載 BY FAITH I MEAN A VISION OF GOOD ONE CHERISHES AND THE ENTHUSIASM THAT PUSHES ONE TO SEEK ITS FULFILLMENT REGARDLESS OF OBSTACLES. BY FAITH I BY FAITH