上課講義 - 網路資料庫實驗室
上課講義 - 網路資料庫實驗室
上課講義 - 網路資料庫實驗室
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Java 程式設計<br />
基礎班 (7)<br />
劉根豪<br />
台大電機所網路資料庫研究室<br />
Class 7 1<br />
Java Data Structure<br />
Email: kenliu@arbor.ee.ntu.edu.tw<br />
動態資料結構<br />
執行的時候可以動態變大或縮小<br />
類型<br />
Linked lists<br />
Class 7<br />
圖示<br />
Class 7<br />
Stacks<br />
Queues<br />
Binary trees<br />
15 10<br />
3<br />
5<br />
回顧<br />
Java I/O<br />
Class 7<br />
自我參考類別(self-referential<br />
classes)<br />
包含參考到相同類別物件的實體欄位<br />
class Node {<br />
private int data;<br />
private Node nextNode; // reference to next linked node<br />
}<br />
欄位 nextNode 為連結<br />
nextNode 連結到另一個Node object<br />
Class 7<br />
動態記憶體配置<br />
在執行期要求更多的記憶體來儲存新的物<br />
件<br />
JVM的記憶體是一個可改變大小的heap<br />
structure<br />
可以用-Xms和-Xmx來跟JVM要求起始配置的<br />
heap大小, e.g., java –Xmx1600mb myclass<br />
WindowsXP+JRE1.4.0最大可以到16xxmb左<br />
右<br />
Class 7<br />
2<br />
4<br />
6<br />
1
Linked Lists<br />
Linked list<br />
Class 7<br />
Linear collection of self-referential classes<br />
(nodes)<br />
Connected by reference links<br />
Nodes可以insert或delete,即使它不在list的開<br />
頭或結尾<br />
Last node is set to null to mark end of list<br />
20.4 Linked Lists (cont.)<br />
Class 7<br />
(a)<br />
(b)<br />
firstNode<br />
new Listnode<br />
firstNode<br />
new Listnode<br />
7 11<br />
Graphical representation of operation insertAtFront.<br />
12<br />
7 11<br />
20.4 Linked Lists (cont.)<br />
Class 7<br />
(a)<br />
(b)<br />
removeItem<br />
firstNode<br />
firstNode<br />
12<br />
12<br />
lastNode<br />
12 7 11 5<br />
lastNode<br />
7 11 5<br />
Graphical representation of operation removeFromFront.<br />
7<br />
9<br />
11<br />
Linked Lists<br />
Class 7<br />
Class 7<br />
firstNode<br />
lastNode<br />
...<br />
H D Q<br />
20.4 Linked Lists (cont.)<br />
(a)<br />
firstNode<br />
12<br />
lastNode<br />
12 7 11 5<br />
new Listnode<br />
(b) firstNode lastNode new Listnode<br />
7 11 5<br />
Graphical representation of operation insertAtBack.<br />
20.4 Linked Lists (cont.)<br />
Class 7<br />
(a)<br />
(b)<br />
firstNode<br />
12<br />
lastNode<br />
12 7 11 5<br />
firstNode current<br />
lastNode<br />
7 11 5<br />
removeItem<br />
Graphical representation of operation removeFromBack.<br />
8<br />
10<br />
12<br />
2
Queues<br />
Queue<br />
先進先出<br />
可以想像一個supermarket的購物checkout List<br />
Nodes inserted only at tail (back)<br />
Method enqueue<br />
Nodes removed only from head (front)<br />
Method dequeue<br />
Class 7<br />
Trees<br />
Tree<br />
Non-linear, two-dimensional data structure<br />
(有別於 linked lists, stacks and queues)<br />
每一個Node有兩個(或以上)的Links<br />
Root node is the first node<br />
Each link refers to a child<br />
Left child is the first node in left subtree<br />
Right child is the first node in right subtree<br />
Children of a specific node are siblings<br />
Nodes with no children are leaf nodes<br />
Class 7<br />
Binary Tree<br />
Class 7<br />
B<br />
A D<br />
Binary tree graphical representation.<br />
C<br />
13<br />
15<br />
17<br />
Stacks<br />
先進後出<br />
E.g., 停車<br />
Class 7<br />
Binary search tree<br />
Special ordering of nodes<br />
Values in left subtrees are less than values in right subtrees<br />
Inorder traversal<br />
Traverse left subtree, obtain node value, traverse right subtree<br />
Preorder traversal<br />
Obtain node value, traverse left subtree, traverse right subtree<br />
Postorder traversal<br />
Traverse left subtree, traverse right subtree, obtain node value<br />
Class 7<br />
Tree Traversals<br />
見<br />
http://www.student.seas.gwu.edu/~idsv/ids<br />
v.html<br />
Class 7<br />
14<br />
16<br />
18<br />
3
Tree Travesals<br />
InOrder7,11,17,25,31,43,44,47…<br />
PreOrder47,25,11,7,17,43,31,44…<br />
PostOrder7,17,11,31,44,43,25…<br />
Class 7<br />
47<br />
25 77<br />
11 43 65 93<br />
7 17 31 44 68<br />
Binary search tree containing 12 values.<br />
Collections Overview<br />
Collection<br />
Data structure (object) that can hold references to<br />
other objects<br />
Collections framework<br />
Interfaces declare operations for various collection<br />
types<br />
Belongs to package java.util<br />
Class 7<br />
Collections Overview<br />
Class 7<br />
19<br />
21<br />
23<br />
Java collections framework<br />
Java collections framework<br />
Provides reusable componentry<br />
Common data structures<br />
Example of code reuse<br />
Class 7<br />
Collections<br />
Built-in support for collections<br />
Similar to STL in C++<br />
Collection type<br />
Sequence/Set<br />
Example ArrayList<br />
Map type<br />
Hashtable/dictionary<br />
Example HashMap<br />
Collections store pointers to objects!<br />
Use inheritance and interfaces<br />
Class 7<br />
Collection Design<br />
All classes implement a similar interface<br />
add(), size(), iterator()…<br />
Easy learning curve for using Collections<br />
Possible to swap out the underlying implementation without<br />
significant code change<br />
Implemented as pointer to Object<br />
Similar to using a void * in C<br />
Require a cast back to the actual type<br />
Example<br />
String element = (String)arraylist.get(i)<br />
Java checks all casts at run-time<br />
Class 7<br />
20<br />
22<br />
24<br />
4
Collection Messages<br />
Basic messages<br />
constructor()<br />
Class 7<br />
Creates a collection with no elements<br />
size()<br />
Number of elements in the collection<br />
boolean add()<br />
Add a new pointer/element at the end of the collection<br />
Returns true is the collection is modified.<br />
iterator()<br />
Returns an Iterator Object<br />
Iterators<br />
Used to iterate through a collection<br />
Abstracts away the underlying details of the<br />
implementation<br />
Iterating through an array is the same as a binary tree<br />
Responds to<br />
hasNext() - Returns true if more elements<br />
next() - Returns the next element<br />
remove() - removes element returned by previous<br />
next() call.<br />
Class 7<br />
Lists<br />
List<br />
Ordered Collection that can contain<br />
duplicate elements<br />
Sometimes called a sequence<br />
Implemented via interface List<br />
ArrayList<br />
Class 7<br />
LinkedList<br />
Vector<br />
25<br />
27<br />
29<br />
Additional Collection Messages<br />
Utilities<br />
Additional useful methods<br />
boolean isEmpty()<br />
boolean contains(Object o)<br />
Iterative search, uses equals()<br />
boolean remove(Object o)<br />
Iterative remove(), uses equals()<br />
Boolean addAll(Collection c)<br />
Class 7<br />
Working with Iterators<br />
Not valid to modify a collection directly<br />
while an iterator is being used!<br />
Should not call collection.add() or<br />
collection.remove()<br />
OK to modify the collection using the<br />
iterator itself<br />
iterator.remove()<br />
Why?<br />
Class 7<br />
Vector<br />
Class 7<br />
26<br />
28<br />
30<br />
5
ArrayList<br />
Most useful collection<br />
Replaces the “Vector” class<br />
Can grow over time<br />
Methods<br />
add()<br />
int size()<br />
Object get(int index)<br />
Index is from 0 to size() -1<br />
Must cast to appropriate type when used.<br />
iterator()<br />
We’ll see an ezample!<br />
Class 7<br />
ArrayList Demo: adding/size<br />
Class 7<br />
// add things...<br />
for (int i= 0; i
ArrayList Demo: output<br />
/* Output...<br />
size:10<br />
0<br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
to string:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br />
size:0<br />
*/<br />
Class 7<br />
Sets<br />
Set<br />
Class 7<br />
Collection that contains unique elements<br />
HashSet<br />
Stores elements in hash table<br />
TreeSet<br />
Stores elements in tree<br />
Unmodifiable Collection<br />
Unmodifiable Collection<br />
將一個collection轉成不可被修改的collection<br />
假設嘗試要modifiy它的話,系統會Throw<br />
UnsorrtedOperationException<br />
Class 7<br />
37<br />
39<br />
41<br />
Algorithms<br />
在Collections物件中提供了一些對collection的基本運作algorithms<br />
Implemented as static methods<br />
List algorithms<br />
sort<br />
binarySearch<br />
reverse<br />
shuffle<br />
fill<br />
copy<br />
Collection algorithms<br />
Min,max<br />
在Arrays物件中提供了一些對array運作的函式<br />
Class 7<br />
Maps<br />
Map<br />
Associates keys to values<br />
Cannot contain duplicate keys<br />
Called one-to-one mapping<br />
Class 7<br />
要有hashcode()才行<br />
38<br />
40<br />
7