http www csie nctu edu twtsaiwnjava Programming in

  • Slides: 58
Download presentation
http: //www. csie. nctu. edu. tw/~tsaiwn/java/ Programming in Java More examples, … 蔡文能 交通大學資訊

http: //www. csie. nctu. edu. tw/~tsaiwn/java/ Programming in Java More examples, … 蔡文能 交通大學資訊 程學系 tsaiwn@csie. nctu. edu. tw 交通大學資訊 程學系

Java More Java Examples Agenda Computing factorial - Loop method - Recursive method Computing

Java More Java Examples Agenda Computing factorial - Loop method - Recursive method Computing primes - Sieve method Sorting an array - Selection Sort Insertion Sort Bubble Sort Quick Sort Merge Sort 交通大學資訊 程學系 蔡文能 7 -第 2頁

Java More Java Examples Computing Factorial • The factorial of an integer is the

Java More Java Examples Computing Factorial • The factorial of an integer is the product of that number and all of the positive integers smaller than it. - 0! = 1 - 1! = 1. . . - 5! = 5*4*3*2*1 = 120 - 6! = 6*5*4*3*2*1 = 720. . . - 50! = 30414093201713378043612608166064768844377641568960512000000 交通大學資訊 程學系 蔡文能 7 -第 3頁

Java More Java Examples Factorial -- Iteration public class Factorial { public static long

Java More Java Examples Factorial -- Iteration public class Factorial { public static long factorial(int n) { long fact = 1; for (int i =2; i <= n; i ++) fact *= i; return fact; } } //for Loop //shorthand for fact=fact*i; In separate files public class Computing. Factorial { public static void main(String arg[ ]) { long a = Factorial. factorial(Integer. parse. Int(arg[0])); System. out. println("Factorial of " + arg[0] + "=" + a); } } 交通大學資訊 程學系 蔡文能 7 -第 4頁

Java More Java Examples Factorial – Recursion, in Java /** * This class shows

Java More Java Examples Factorial – Recursion, in Java /** * This class shows a recursive method to compute factorials. This method * calls itself repeatedly based on the formula: n! = n* (n-1)! **/ public class Factorial 2 { public static long factorial(int n) { if (n == 1) return 1; else return n*factorial(n - 1); } } 交通大學資訊 程學系 蔡文能 7 -第 6頁

Java More Java Examples N Factorial ( Recursive版 ) long factorial(int n) { if(

Java More Java Examples N Factorial ( Recursive版 ) long factorial(int n) { if( n < 0) return -factorial(-n); if( n ==1 || n==0) return 1; return n * factorial(n-1); } /*告訴我 n-1 階乘, 我就給你 n 階乘*/ #include <stdio. h> int main( ){ printf("5! = %ldn", factorial(5)); } 交通大學資訊 程學系 蔡文能 C語言版本 N階乘就是N乘以N-1階乘 7 -第 7頁

Java More Java Examples GCD -- 歐幾里得的最大公約數 輾轉相除法 (recursive概念!) GCD(m, n) = GCD(n, m%n)

Java More Java Examples GCD -- 歐幾里得的最大公約數 輾轉相除法 (recursive概念!) GCD(m, n) = GCD(n, m%n) 但如果 n 是 0 則答案為 m long gcd(long m, long n) { if(n==0) return m; return gcd(n, m%n); } 如何寫成non-recursive version ? 交通大學資訊 程學系 蔡文能 7 -第 8頁

Java More Java Examples GCD non-recursive version long gcd(long m, long n) { int

Java More Java Examples GCD non-recursive version long gcd(long m, long n) { int r; /* remainder */ while(n != 0) { r = m%n; m = n; n = r; } return m; } 交通大學資訊 程學系 蔡文能 7 -第 9頁

Java More Java Examples 問題與思考 (Recursion) 寫出 non-recursive 版的Fibonacci function ? 考慮小兔子隔兩個月可以長大為成兔 ? 考慮成兔的懷孕期是兩個月

Java More Java Examples 問題與思考 (Recursion) 寫出 non-recursive 版的Fibonacci function ? 考慮小兔子隔兩個月可以長大為成兔 ? 考慮成兔的懷孕期是兩個月 ? 其他 Recursive 問題 - Recursive 版的 binary search Quick sort Hanoi tower problem … 交通大學資訊 程學系 蔡文能 7 -第 13頁

Java More Java Examples Factorial – cache ans in a Table public class Factorial

Java More Java Examples Factorial – cache ans in a Table public class Factorial 3 { //create an array to cache values 0! Through 20! static long[ ] table = new long[21]; static {ans[0] = 1; } //factorial of 0 is 1 static int last = 0; public static long factorial(int n) { if(n <= last) return ans[n]; long tmp = ans[last]; int k = last; while (k < n) { tmp = (k+1) * tmp; /* (k+1) ! */ if(last < 20) {++last; ans [last ] = tmp; } ++k; } /* while */ return tmp; } Cache 唸作 cash } 交通大學資訊 程學系 蔡文能 7 -第 14頁

Java More Java Examples Computing Primes (1/3) • Finding the largest prime number smaller

Java More Java Examples Computing Primes (1/3) • Finding the largest prime number smaller than a specified integer: Input integer m, find p m such that p is a prime and if there is prime p’ > p then p’ must be larger than m. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 • Finding all prime numbers that smaller than a specified integer? 交通大學資訊 程學系 蔡文能 7 -第 15頁

Java More Java Examples Computing Primes (2/3) • Algorithm main idea: find primes by

Java More Java Examples Computing Primes (2/3) • Algorithm main idea: find primes by eliminating multiples of the form k j, where j is a prime smaller than square-root(m) and k is an integer such that k j m. 2 prime . . . i . . . 2 2 2 i 2 j 3 2 3 i 3 j 4 2 4 i 4 j square-root(m) . . 交通大學資訊 程學系 蔡文能 j 7 -第 16頁

Java More Java Examples Computing Primes (3/3) • Finding all prime numbers that smaller

Java More Java Examples Computing Primes (3/3) • Finding all prime numbers that smaller than a specified integer? 1 2 3 4 5 6 7 交通大學資訊 程學系 蔡文能 8 9 10 11 12 13 14 15 16 17 18 19 20 7 -第 17頁

Java More Java Examples Example: Find Primes (1/2) Import java. lang. *; public class

Java More Java Examples Example: Find Primes (1/2) Import java. lang. *; public class Sieve { public static void main(String[] args) { int max = 100; //Assign a default value try {max = Integer. parse. Int(arg[0]); } catch (Exception e) {} //Silently ignore exceptions. //Create an array that specifies whether each number is prime or not. Boolean[] isprime = new boolean[max+1]; //Assume that all numbers are primes, until proven otherwise. for (int i = 0; <= max; i++) isprime[i] = true; //We know that 0 and 1 are not prime. Make a note of it. isprime[0] = isprime[1] = false; 交通大學資訊 程學系 蔡文能 7 -第 18頁

Java More Java Examples Example: Find Primes (2/2) //To compute all primes less than

Java More Java Examples Example: Find Primes (2/2) //To compute all primes less than max, we need to rule out multiples of all //integers less than the square root of max. int n = (int) Math. ceil(Math. sqrt(max)); for (int i = 0; i <= n; i++) { if (isprime[i]) { int k = 2; for (int j = k*i; j <= max; j = (k ++)*i) isprime[j] = false; } } int largest; for (largest = max; !sprime[largest]; largest--); //empty loop body System. out. println("The largest prime less than or equal to " + max + " is " + largest); } } 交通大學資訊 程學系 蔡文能 7 -第 19頁

Java More Java Examples Sorting Numbers • Sorting: Input n numbers, sort them such

Java More Java Examples Sorting Numbers • Sorting: Input n numbers, sort them such that the numbers are ordered increasingly. 3 9 1 6 5 4 8 2 10 7 1 2 3 4 5 6 7 8 9 10 交通大學資訊 程學系 蔡文能 7 -第 20頁

Java More Java Examples Selection Sort (1/4) • A simple sorting algorithm Purpose: Input:

Java More Java Examples Selection Sort (1/4) • A simple sorting algorithm Purpose: Input: an array A containing n integers. Output: sorted array. 1. i : = 2; 2. Find the least element a from A(i) to A(n); 3. If a is less than A(i - 1), exchange A(i - 1) and a; 4. i : = i + 1; if (i <= n) goto step (2). 交通大學資訊 程學系 蔡文能 7 -第 21頁

Java More Java Examples Selection Sort (2/4) • swap 1 st step: 3 9

Java More Java Examples Selection Sort (2/4) • swap 1 st step: 3 9 1 6 5 4 8 2 10 7 swap 2 nd step: 1 9 3 6 5 4 8 2 10 7 1 2 3 6 5 4 8 9 10 7 …. . . 交通大學資訊 程學系 蔡文能 7 -第 22頁

Java More Java Examples Selection Sort (3/4) • Sorting program: public class Sel. Sort

Java More Java Examples Selection Sort (3/4) • Sorting program: public class Sel. Sort { public static void sort(double[ ] nums) { for(int i = 0; i < nums. length; i++) { int min = i +1; for (int j = i+2; j < nums. length; j++) { if (nums[j] < nums[min]) min = j; } if (nums[i] > nums[min]) { double tmp; tmp = nums[i]; nums[i] = nums[min]; nums[min] = tmp; } } 交通大學資訊 程學系 蔡文能 7 -第 23頁

Java More Java Examples Selection Sort (4/4) • In separate file: public class Sort.

Java More Java Examples Selection Sort (4/4) • In separate file: public class Sort. Number { public static void main (String[ ] args) { double[ ] nums = new double[10]; //Create an array to hold numbers for(int i = 0; i < nums. length; i++) //Generate random numbers nums[i] = Math. random( )*100; Sel. Sort. sort(nums); //Sort them for (int j = 0; j < nums. length; j++) //Print them out System. out. println(nums [j] ); } } Math. random( ) 交通大學資訊 程學系 蔡文能 : see next slide 7 -第 24頁

Java More Java Examples Insertion Sort public class Ins. Sort { public static void

Java More Java Examples Insertion Sort public class Ins. Sort { public static void sort(double[ ] nums) { for(int i = 1; i < nums. length; i++) { double tmp = nums[i]; /* we want to put this in proper position */ k = i – 1; while ((k>=0) && nums[k] > tmp ) { /* smaller than me */ nums[k+1] = nums[k]); /* move it down */ --k; } nums[k+1] = tmp; /* copy it into correct position */ } } } 交通大學資訊 程學系 蔡文能 7 -第 25頁

Java More Java Examples Test the Insertion Sort • In separate file as previous

Java More Java Examples Test the Insertion Sort • In separate file as previous example (Selection sort): public class Sort. Number { public static void main (String[ ] args) { double[ ] nums = new double[10]; //Create an array to hold numbers for(int i = 0; i < nums. length; i++) //Generate random numbers nums[i] = Math. random( )*100; Ins. Sort. sort(nums); //Sort them using insertion sort for (int j = 0; j < nums. length; j++) //Print them out System. out. println(nums [j] ); } } 交通大學資訊 程學系 蔡文能 7 -第 26頁

Java More Java Examples Bubble Sort public class Bub. Sort { public static void

Java More Java Examples Bubble Sort public class Bub. Sort { public static void sort(double[ ] nums) { for(int i = nums. length-2; i >=0; --i) { int flag = 0; /* Assume no exchange */ for(k=0; k <= i; ++k) { /* walk through all every pass */ if ( nums[k] > nums[k+1] ) { /* incorrect order */ double tmp=nums[k]; nums[k]=nums[k+1]; nums[k+1]=tmp; ++flag; } } if(flag == 0) break; /* no need to do next pass */ } } } 交通大學資訊 程學系 蔡文能 7 -第 27頁

Java More Java Examples Quick Sort (1/6) Algorithm quick_sort(array A, from, to) Input: from

Java More Java Examples Quick Sort (1/6) Algorithm quick_sort(array A, from, to) Input: from - pointer to the starting position of array A to - pointer to the end position of array A Output: sorted array: A’ 1. 2. 3. 4. 5. 6. 7. Choose any one element as the pivot; Find the first element a = A[i] larger than or equal to pivot from A[from] to A[to]; Find the first element b = A[j] smaller than or equal to pivot from A[to] to A[from]; If i < j then exchange a and b; Repeat step from 2 to 4 until j <= i; If from < j then recursive call quick_sort(A, from, j); If i < to then recursive call quick_sort(A, i, to); 交通大學資訊 程學系 蔡文能 7 -第 28頁

Java More Java Examples Quick Sort (2/6) • Quick sort Choose 5 as pivot

Java More Java Examples Quick Sort (2/6) • Quick sort Choose 5 as pivot main idea: from 1 st step: 3 9 1 6 5 4 8 2 to 10 j i 2 nd step: 3 2 1 6 5 4 8 3 rd step: 3 2 1 4 5 6 8 Smaller than any integer right to 5 交通大學資訊 程學系 蔡文能 7 9 9 10 7 greater than any integer left to 5 7 -第 29頁

Java More Java Examples Quick Sort (3/6) from • Quick sort from pivot to

Java More Java Examples Quick Sort (3/6) from • Quick sort from pivot to 4 th step: 3 2 1 4 5 5 th step: 1 6 th step: 7 th step: 8 th step: 交通大學資訊 程學系 蔡文能 2 3 to pivot 4 6 10 9 8 7 5 5 6 7 8 10 9 9 10 7 -第 30頁

Java More Java Examples Quick Sort (4/6) public class Quick. Sorter { public static

Java More Java Examples Quick Sort (4/6) public class Quick. Sorter { public static void sort (int[ ] a, int from, int to) { if ((a == null) || (a. length < 2)) return; int i = from, j = to; int pivot = a[(from + to)/2]; do { while ((i < to) && (a[i] < pivot)) i++; while ((j > from) && (a[j] >= pivot)) j--; if (i < j) { int tmp =a[i]; a [i] = a[j]; a[j] = tmp; } i++; j--; }while (i <= j); if (from < j) sort(a, from, j); if (i < to) sort(a, i, to); } } 交通大學資訊 程學系 蔡文能 7 -第 31頁

Java More Java Examples Quick Sort (5/6) 3, 4, 6, 1, 10, 9, 5,

Java More Java Examples Quick Sort (5/6) 3, 4, 6, 1, 10, 9, 5, 20, 19, 14, 12, 2, 15, 21, 13, 18, 17, 8, 16, 1 3, 4, 6, 1, 10, 9, 5, 20, 19, 1, 12, 2, 15, 21, 13, 18, 17, 8, 16, 14 j i 3, 4, 6, 1, 10, 9, 5, 8, 19, 1, 12, 2, 15, 21, 13, 18, 17, 20, , 16, 14 i j 3, 4, 6, 1, 10, 9, 5, 8, 13 , 1, 12, 2, 15, 21, 19, 18, 17, 20, 16, 14 i j 3, 4, 6, 1, 10, 9, 5, 8, 13 , 1, 12, 2, 14 21, 19, 18, 17, 20, 16, 15 i 3, 4, 6, 1, 10, 9, 5, 8, 13 , 1, 12, 2 j 交通大學資訊 程學系 蔡文能 7 -第 32頁

Java More Java Examples Quick Sort (6/6) public class BQSorter { public static void

Java More Java Examples Quick Sort (6/6) public class BQSorter { public static void sort (int[ ] a, int from, int to) { if ((a == null) || (a. length < 2) || from >= to) return; int k = (from + to)/2; int tmp =a[to]; a [to] = a[k]; a[k] = tmp; int pivot = a[to]; int i = from, j = to-1; while(i < j ) { while ((i < j) && (a[i] < pivot)) i++; while ((i < j) && (a[j] >= pivot)) j--; if (i < j) { tmp =a[i]; a [i] = a[j]; a[j] = tmp; } }; tmp =a[i]; a [i] = a[to]; a[to] = tmp; if (from < i-1) sort(a, from, i-1); if (i < to) sort(a, i+1, to); } } 交通大學資訊 程學系 蔡文能 7 -第 33頁

Java More Java Examples Merge Sort (1/3) Merging means the combination of two or

Java More Java Examples Merge Sort (1/3) Merging means the combination of two or more ordered sequence into a single sequence. For example, can merge two sequences: 503, 765 and 087, 512, 677 to obtain a sequence: 087, 503, 512, 677, 703, 765. A simple way to accomplish this is to compare the two smallest items, output the smallest, and then repeat the same process. 503 087 703 512 765 677 087 交通大學資訊 程學系 蔡文能 503 512 703 677 765 677 7 -第 34頁

Java More Java Examples Merge Sort (2/3) Algorithm Merge(s 1, s 2) Input: two

Java More Java Examples Merge Sort (2/3) Algorithm Merge(s 1, s 2) Input: two sequences: s 1 - x 1 x 2. . . xm and s 2 - y 1 y 2. . . yn Output: a sorted sequence: z 1 z 2. . . zm+n. 1. [initialize] i : = 1, j : = 1, k : = 1; 2. [find smaller] if xi yj goto step 3, otherwise goto step 5; 3. [output xi] zk. : = xi, k : = k+1, i : = i+1. If i m, goto step 2; 4. [transmit yj . . . yn] zk, . . . , zm+n : = yj, . . . , yn. Terminate the algorithm; 5. [output yj] zk. : = yj, k : = k+1, j : = j+1. If j n, goto step 2; 6. [transmit xi . . . xm] zk, . . . , zm+n : = xi, . . . , xm. Terminate the algorithm; 交通大學資訊 程學系 蔡文能 7 -第 35頁

Java More Java Examples Merge Sort (3/3) Algorithm Merge-sorting(s) Input: a sequences s =

Java More Java Examples Merge Sort (3/3) Algorithm Merge-sorting(s) Input: a sequences s = < x 1, . . . , xm> Output: a sorted sequence. 1. If |s| = 1, then return s; 2. k : = m/2 ; 3. s 1 : = Merge-sorting(x 1, . . . , xk); 4. s 2 : = Merge-sorting(xk+1, . . . , xm); 5. return(Merge(s 1, s 2)); 交通大學資訊 程學系 蔡文能 7 -第 36頁

Java More Java Examples Time complexity of Mergesort Takes roughly n·log 2 n comparisons.

Java More Java Examples Time complexity of Mergesort Takes roughly n·log 2 n comparisons. Without the shortcut, there is no best or worst case. With the optional shortcut, the best case is when the array is already sorted: takes only (n-1) comparisons. 交通大學資訊 程學系 蔡文能 7 -第 37頁

Java More Java Examples Binary Search (1/5) Works for an array of numbers or

Java More Java Examples Binary Search (1/5) Works for an array of numbers or objects that can be compared and are arranged in ascending (or descending) order. A very fast method: only 20 comparisons are needed for an array of 1, 000 elements; (30 comparisons can handle 1, 000, 000 elements; etc. ) 交通大學資訊 程學系 蔡文能 7 -第 38頁

Java More Java Examples Binary Search (2/5) Main idea: “divide and conquer”: - compare

Java More Java Examples Binary Search (2/5) Main idea: “divide and conquer”: - compare the target value to the middle element; if equal, all set if smaller, apply binary search to the left half; if larger, apply binary search to the right half. NJ v. . . MI MN MO MS MT NC ND . . . NJ. . . MI MN 交通大學資訊 程學系 蔡文能 MO MS MT 7 -第 39頁

Java More Java Examples Binary Search (3/5) Recursive implementation: public int binary. Search (int

Java More Java Examples Binary Search (3/5) Recursive implementation: public int binary. Search (int arr [ ], int value, int left, int right) { int middle = (left + right) / 2; if (value == arr [middle] ) return middle; else if ( value < arr[middle] && left < middle ) return binary. Search (arr, value, left, middle - 1); else if ( value > arr[middle] && middle < right ) return binary. Search (arr, value, middle + 1, right ); else return -1; // Not found 交通大學資訊 程學系 蔡文能 } 7 -第 40頁

Java More Java Examples Binary Search (4/5) Iterative implementation: public int binary. Search (int

Java More Java Examples Binary Search (4/5) Iterative implementation: public int binary. Search (int arr [ ], int value, int left, int right) { while (left <= right) { int middle = (left + right) / 2; if ( value == arr [middle] ) return middle; else if ( value < arr[middle] ) right = middle - 1; else /* if ( value > arr[middle] ) */ left = middle + 1; } return -1; // Not found 交通大學資訊 程學系 蔡文能 } 7 -第 41頁

Java More Java Examples Binary Search (5/5) The average number of comparisons is roughly

Java More Java Examples Binary Search (5/5) The average number of comparisons is roughly log 2 n The worst-case is log 2 (n + 1) rounded up to an integer (e. g. 2 for n = 3, 3 for n = 7, 4 for n = 15, etc. ) 交通大學資訊 程學系 蔡文能 7 -第 42頁

Java More Java Examples PRNG Pseudo Random Number Generator java. lang. Math. random( )

Java More Java Examples PRNG Pseudo Random Number Generator java. lang. Math. random( ) - A real (double) number: Uniform random number [0, 1) java. util. Random - A utility class in java. util package Has many miscellaneous random # generating functions java. lang. Math. random( ) actually uses next. Double( ) in this class next. Int( ) returns a number like int rand( ) in C language next. Gaussian( ) returns a # in standard Normal distribution 交通大學資訊 程學系 蔡文能 7 -第 43頁

Java More Java Examples java. util. Random (1/2) Benchmarks uses the java. util. Random

Java More Java Examples java. util. Random (1/2) Benchmarks uses the java. util. Random class — a more controlled way to generate random numbers. Constructors: Default “seed”: If we set the same “seed, ” we get the System. current. Time. Millis() same “random” sequence. Random generator = new Random(); Random generator 2 = new Random(seed); 交通大學資訊 程學系 蔡文能 long seed; 7 -第 44頁

Java More Java Examples java. util. Random (2/2) Methods: int k = generator. next.

Java More Java Examples java. util. Random (2/2) Methods: int k = generator. next. Int (); All 232 possible int values are produced with (approximately) equal probability int k = generator. next. Int (n); 0 k<n double x = generator. next. Double (); 0 x<1 交通大學資訊 程學系 蔡文能 7 -第 45頁

Java More Java Examples Normal Distribution Random number 交通大學資訊 程學系 蔡文能 7 -第 46頁

Java More Java Examples Normal Distribution Random number 交通大學資訊 程學系 蔡文能 7 -第 46頁

Java More Java Examples java. util. Arrays Provides static methods for dealing with arrays.

Java More Java Examples java. util. Arrays Provides static methods for dealing with arrays. Works for arrays of numbers, Strings, (and “comparable” Objects). Methods: int pos = Arrays. binary. Search (arr, target); Arrays. sort (arr, from, to); Arrays. fill (arr, value); // fills arr with a given value Arrays. fill (arr, from, to, value); 交通大學資訊 程學系 蔡文能 7 -第 47頁

Java More Java Examples java. util. List Interface The List library interface describes a

Java More Java Examples java. util. List Interface The List library interface describes a list of objects in abstract terms In a list, objects are arranged in sequence obj 0, obj 1, . . . , objn-1 In Java, a list holds references to objects A list can contain duplicate objects (both obj 1. equals(obj 2) and obj 1 == obj 2 ) 交通大學資訊 程學系 蔡文能 7 -第 48頁

Java More Java Examples List Methods (a Subset) int size(); boolean is. Empty ();

Java More Java Examples List Methods (a Subset) int size(); boolean is. Empty (); boolean add (Object obj); returns true void add (int i, Object obj); inserts obj as the i -th value; i must be from 0 to size() Object set(int i, Object obj); Object get(int i); Object remove(int i); boolean contains(Object obj); int index. Of(Object obj); 交通大學資訊 程學系 蔡文能 i must be from 0 to size() -1 use equals to compare objects 7 -第 49頁

Java More Java Examples java. util. Array. List (1/6) Implements List using an array

Java More Java Examples java. util. Array. List (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) capacity size "Cat" "Hat" "Bat" 交通大學資訊 程學系 蔡文能 7 -第 50頁

Java More Java Examples Array. List (2/6) Automatically increases (doubles) the capacity when the

Java More Java Examples Array. List (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 Index. Out. Of. Bounds. Exception when i < 0 or i size() 交通大學資訊 程學系 蔡文能 7 -第 51頁

Java More Java Examples Array. List (3/6) Array. List holds objects (of any type)

Java More Java Examples Array. List (3/6) Array. List 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 交通大學資訊 程學系 蔡文能 7 -第 52頁

Java More Java Examples Array. List (4/6) From Java API Docs: JDK 1. 5:

Java More Java Examples Array. List (4/6) From Java API Docs: JDK 1. 5: Array. List(Collection<? extends E> c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. 交通大學資訊 程學系 蔡文能 7 -第 53頁

Java More Java Examples Array. List (5/6) Example 1 Array. List list = new

Java More Java Examples Array. List (5/6) Example 1 Array. List list = new Array. List (); list. add (new Integer(1)); list. add (new Integer(2)); list. add (new Integer(3)); int sum = 0; for (int i = 0; i < list. size(); i++) sum += ((Integer) list. get(i)). int. Value(); Need a cast to Integer in order to call int. Value 交通大學資訊 程學系 蔡文能 7 -第 54頁

Java More Java Examples Array. List (6/6) Example 2 Array. List words = new

Java More Java Examples Array. List (6/6) Example 2 Array. List words = new Array. List (4); words. add ("One"); words. add ("Fish"); words. add ("Two"); words. add ("Fish"); int i = 0; while (i < words. size() ) { if ( ”Fish". equals (words. get(i)) ) words. remove(i); else i++; } 交通大學資訊 程學系 蔡文能 Shifts all the values after the i-th to the left and decrements the size 7 -第 55頁

Java More Java Examples Generic type 交通大學資訊 程學系 蔡文能 7 -第 56頁

Java More Java Examples Generic type 交通大學資訊 程學系 蔡文能 7 -第 56頁

Java More Java Examples Generic Programming Define software components with type parameters - A

Java More Java Examples Generic Programming Define software components 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 JDK 1. 5: type inference 交通大學資訊 程學系 蔡文能 7 -第 57頁

Java More Java Examples Exception, More examples, … 謝謝捧場 http: //www. csie. nctu. edu.

Java More Java Examples Exception, More examples, … 謝謝捧場 http: //www. csie. nctu. edu. tw/~tsaiwn/java/ 蔡文能 交通大學資訊 程學系 蔡文能 7 -第 58頁