一.數組與Arrays
(1)Arrays的使用
import java.util.Arrays;
public class TestArrays {
public static void main(String[] args) {
int[] a = {44, 77, 55, 22, 99, 88, 33, 66};
print(a);
Arrays.sort(a);
print(a);
int k = Arrays.binarySearch(a, 44);
System.out.printf("Arrays.binarySearch(a, 44): %d%n", k);
System.out.printf("a[%d]: %d%n", k, a[k]);
k = Arrays.binarySearch(a, 45);
System.out.printf("Arrays.binarySearch(a, 45): %d%n", k);
int[] b = new int[8];
print(b);
Arrays.fill(b, 55);
print(b);
System.out.println("Arrays.equals(a,b): " + Arrays.equals(a,b));
}
}
The output is
44 77 55 22 99 88 33 66
22 33 44 55 66 77 88 99
Arrays.binarySearch(a, 44): 2
a[2]: 44
Arrays.binarySearch(a, 45): -4
0 0 0 0 0 0 0 0
55 55 55 55 55 55 55 55
Arrays.equals(a,b): false
(2)順序查找
public class TestBinarySearch {
public static void main(String[] args) {
int[] a = {22, 33, 44, 55, 66, 77, 88, 99};
System.out.println("search(a, 44): " + search(a, 44));
System.out.println("search(a, 50): " + search(a, 50));
System.out.println("search(a, 77): " + search(a, 77));
System.out.println("search(a, 100): " + search(a, 100));
}
public static int search(int[] a, int x) {
// POSTCONDITIONS: returns an integer i;
// if i >= 0, then a[i] == x; otherwise x is not in a[];
for (int i=0; i<a.length; i++) { // step 1
// INVARIANT: x is not among a[0]...a[i-1] // step 2
if (a[i] == x) { // step 3
return i;
}
}
return -1; // step 4
}
}
The output is:
search(a, 44): 2
search(a, 50): -1
search(a, 77): 5
search(a, 100): -1
(3)折半查找
public class TestBinarySearch {
public static void main(String[] args) {
int[] a = {22, 33, 44, 55, 66, 77, 88, 99};
System.out.println("search(a, 44): " + search(a, 44));
System.out.println("search(a, 50): " + search(a, 50));
System.out.println("search(a, 77): " + search(a, 77));
System.out.println("search(a, 100): " + search(a, 100));
}
public static int search(int[] a, int x) {
// POSTCONDITIONS: returns i;
// if i >= 0, then a[i] == x; otherwise i == -1;
int lo = 0;
int hi = a.length;
while (lo < hi) { // step 1
// INVARIANT: if a[j]==x then lo <= j < hi; // step 3
int i = (lo + hi)/2; // step 4
if (a[i] == x) {
return i; // step 5
} else if (a[i] < x) {
lo = i+1; // step 6
} else {
hi = i; // step 7
}
}
return -1; // step 2
}
}
(4)反轉
void reverse(int[] a) {
int n = a.length;
if (n < 2) {
return;
}
for (int i = 0; i < n/2; i++) {
swap(a, i, n-i-1);
}
}
void swap(int[] a, int i, int j) {
// swaps a[i] with a[j]:
int ai = a[i];
int aj = a[j];
a[i] = aj;
a[j] = ai;
}
二.鏈式數據結構
(1)插入有序數組
void insert(int[] a, int n, int x) {
// preconditions: a[0] <= ... <= a[n-1], and n < a.length;
// postconditions: a[0] <= ... <= a[n], and x is among them;
int i = 0;
while (i < n && a[i] <= x) {
++i;
}
System.arraycopy(a, i, a, i+1, n-i); // copies a[i..n) into a[i+1..n+1)
a[i] = x;
}
(2)單向鏈表插入和刪除
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
Node insert(Node start, int x) {
if (start == null || start.data > x) {
start = new Node(x,start);
return start;
}
Node p=start;
while (p.next != null) {
if (p.next.data > x) break;
p = p.next;
}
p.next = new Node(x,p.next);
return start;
}
Node delete(Node start, int x) {
if (start == null || start.data > x) { // x is not in the list
return start;
} else if (start.data == x) { // x is the first element in the list
return start.next;
}
for (Node p = start; p.next != null; p = p.next) {
if (p.next.data > x) {
break; // x is not in the list
} else if (p.next.data == x) { // x is in the p.next node
p.next = p.next.next; // delete it
break;
}
}
return start;
}
三.JAVA容器類
虛線框表示接口。
實線框表示實體類。
粗線框表示最常用的實體類。
點線的箭頭表示實現了這個接口。
實線箭頭表示類可以製造箭頭所指的那個類的對象。
容器類持有對象方式
1, Collection:只允許每個位置上放一個對象。它包括“以一定順序持有一組對象”的List,以及“只能允許添加不重複對象”的Set。你可以用add()方法向Collection對象中加元素。
2, Map:一組以“鍵-值”(key-value)的形式出現的pair,Map也不接受重複的key值。你可以用put()方法往Map裏面加元素。
Collection是個java.util下的接口,它是各種集合結構的父接口。繼承自它的接口主要有Set 和List.
無論使用哪種Set,都需要定義equals(),但是只有在“要把對象放進HashSet”的情況下,才需要定義hashCode().因爲HashSet是我們通常用的Set,所以通常也需要定義hashCode()。作爲一種編程風格,你應該在覆寫equals()的同時把hashCode()也覆寫了。
LinkedList類
LinkedList使用雙向鏈表來實現的,也就是每個對象,除了保存數據之外,還保存着在它前面和後面的那兩個對象的reference,允許null元素。此外LinkedList提供額外的get,remove,insert方法在LinkedList的首部或尾部。這些操作使LinkedList可被用作堆棧(stack),隊列(queue)或雙向隊列(deque)。
注意LinkedList沒有同步方法。如果多個線程同時訪問一個List,則必須自己實現訪問同步。一種解決方法是在創建List時構造一個同步的List:
List list = Collections.synchronizedList(new LinkedList(...));
ArrayList類
ArrayList擅長對元素進行隨機訪問,可以把它想成“一個能夠自動擴展的數組”。它允許所有元素,包括null。ArrayList沒有同步。
size,isEmpty,get,set方法運行時間爲常數。但是add方法開銷爲分攤的常數,添加n個元素需要O(n)的時間。其他的方法運行時間爲線性。
每個ArrayList實例都有一個容量(Capacity),即用於存儲元素的數組的大小。這個容量可隨着不斷添加新元素而自動增加,但是增長算法並沒有定義。當需要插入大量元素時,在插入前可以調用ensureCapacity方法來增加ArrayList的容量以提高插入效率。
和LinkedList一樣,ArrayList也是非同步的(unsynchronized)。
Vector類(legacy,遺留的)
Vector非常類似ArrayList。
ArrayList和Vector的區別
一.同步性:Vector是線程安全的,也就是說是同步的,而ArrayList是線程序不安全的,不是同步的。由Vector創建的Iterator,雖然和ArrayList創建的Iterator是同一接口,但是,因爲Vector是同步的,當一個Iterator被創建而且正在被使用,另一個線程改變了Vector的狀態(例如,添加或刪除了一些元素),這時調用Iterator的方法時將拋出ConcurrentModificationException,因此必須捕獲該異常。
二.數據增長:當需要增長時,Vector默認增長爲原來一培,而ArrayList卻是原來的一半.
Stack 類(legacy,遺留的)
Stack繼承自Vector,實現一個後進先出的堆棧。Stack提供5個額外的方法使得Vector得以被當作堆棧使用。基本的push和pop方法,還有peek方法得到棧頂的元素,empty方法測試堆棧是否爲空,search方法檢測一個元素在堆棧中的位置。Stack剛創建後是空棧。
iterator()方法
Enumeration接口定義了可以對一個對象的類集中的元素進行枚舉(一次獲得一個)的方法。這個接口儘管沒有被擯棄,但已經被Iterator所替代。
不論Collection的實際類型如何,它都支持一個iterator()的方法,該方法返回一個迭代子,使用該迭代子即可逐一訪問Collection中每一個元素。
iterator()方法不必知道對象序列的具體實現,就能在序列中移動,選取其中的對象
用iterator()方法叫容器傳給你一個Iterator對象。
List simple=new ArrayList();Iterator e= simple.iterator();
第一次調用Iterator的next()方法的時候,它會傳給你序列中的第一個元素。
用next()方法獲取序列中的下一個對象。
用hasNext()方法查詢序列中是否還有其它對象。
用remove()方法刪除迭代器所返回的最後一個元素。
Hashtable類(legacy,遺留的)
Hashtable繼承Map接口,實現一個key-value映射的哈希表。任何非空(non-null)的對象都可作爲key或者value。
添加數據使用put(key, value),取出數據使用get(key),這兩個基本操作的時間開銷爲常數。
Hashtable通過initial capacity和load factor兩個參數調整性能。通常缺省的load factor 0.75較好地實現了時間和空間的均衡。增大load factor可以節省空間但相應的查找時間將增大,這會影響像get和put這樣的操作。
使用Hashtable的簡單示例如下,將1,2,3放到Hashtable中,他們的key分別是”one”,”two”,”three”:
Hashtable numbers = new Hashtable();
numbers.put(“one”, new Integer(1));
numbers.put(“two”, new Integer(2));
numbers.put(“three”, new Integer(3));
要取出一個數,比如2,用相應的key:
Integer n = (Integer)numbers.get(“two”);
System.out.println(“two = ” + n);
由於作爲key的對象將通過計算其散列函數來確定與之對應的value的位置,因此任何作爲key的對象都必須實現hashCode和equals方法。
hashCode()是Object根類的方法(缺省情況下返回對象的內存地址),因此所有java對象都能生成hash code。
equals()是Object根類的方法,只是簡單地比較兩個對象的地址。
如果你用自定義的類當作key的話,要相當小心,按照散列函數的定義,如果兩個對象相同,即obj1.equals(obj2)=true,則它們的hashCode必須相同,但如果兩個對象不同,則它們的hashCode不一定不同,如果兩個不同對象的hashCode相同,這種現象稱爲衝突,衝突會導致操作哈希表的時間開銷增大,所以儘量定義好的hashCode()方法,能加快哈希表的操作。
如果相同的對象有不同的hashCode,對哈希表的操作會出現意想不到的結果(期待的get方法返回null),要避免這種問題,只需要牢記一條:要同時複寫equals方法和hashCode方法,而不要只寫其中一個。
HashMap類
HashMap和Hashtable類似,不同之處在於HashMap是非同步的,並且允許null,即null value和null key。但是將HashMap視爲Collection時(values()方法可返回Collection),其迭代子操作時間開銷和HashMap的容量成比例。因此,如果迭代操作的性能相當重要的話,不要將HashMap的初始化容量設得過高,或者load factor過低。
hashCode()與equals()方法
hashCode()是Object根類的方法(缺省情況下返回對象的內存地址),因此所有java對象都能生成hash code。HashMap利用對象的hashCode()來進行快速的查找。
equals()是Object根類的方法,只是簡單地比較兩個對象的地址。
無論使用哪種Set,都需要定義equals(),但是只有在“要把對象放進HashSet”的情況下,才需要定義hashCode().因爲HashSet是我們通常用的Set,所以通常也需要定義hashCode()。
如果你不覆寫鍵的hashCode()和equals()的話,散列數據結構(HashSet,HashMap,LinkedHashSet,LinkedHashMap)就沒法正確的處理鍵。
轉載:http://blog.csdn.net/hsttmht/article/details/6989911
四.二叉樹
public class BinaryTree<E> extends AbstractCollection {
protected E root;
protected BinaryTree<E> left, right, parent;
protected int size;
public BinaryTree() {
}
public BinaryTree(E root) {
this.root = root;
size = 1;
}
public BinaryTree(E root, BinaryTree<E> left, BinaryTree<E> right) {
this(root);
if (left != null) {
this.left = left;
left.parent = this;
size += left.size();
}
if (right != null) {
this.right = right;
right.parent = this;
size += right.size();
}
}
}
public class BinaryTree<E> extends AbstractCollection {
// insert lines 2-49 from Example 11.20 on page 212
public Iterator iterator() {
return new PreOrder();
}
abstract public class BinaryTreeIterator implements Iterator {
protected boolean rootDone;
protected Iterator lIt, rIt; // child iterators
public boolean hasNext() {
return !rootDone || lIt != null && lIt.hasNext()
|| rIt != null && rIt.hasNext();
}
abstract public Object next();
public void remove() {
throw new UnsupportedOperationException();
}
}
public class PreOrder extends BinaryTreeIterator {
public PreOrder() {//前序
if (left != null) {
lIt = left.new PreOrder();
}
if (right != null) {
rIt = right.new PreOrder();
}
}
public Object next() {
if (!rootDone) {
rootDone = true;
return root;
}
if (lIt != null && lIt.hasNext()) {
return lIt.next();
}
if (rIt != null && rIt.hasNext()) {
return rIt.next();
}
return null;
}
}
public class InOrder extends BinaryTreeIterator {
public InOrder() {//中序遍歷
if (left != null) {
lIt = left.new InOrder();
}
if (right != null) {
rIt = right.new InOrder();
}
}
public Object next() {
if (lIt != null && lIt.hasNext()) {
return lIt.next();
}
if (!rootDone) {
rootDone = true;
return root;
}
if (rIt != null && rIt.hasNext()) {
return rIt.next();
}
return null;
}
}
public class PostOrder extends BinaryTreeIterator {//後序遍歷
public PostOrder() {
if (left != null) {
lIt = left.new PostOrder();
}
if (right != null) {
rIt = right.new PostOrder();
}
}
public Object next() {
if (lIt != null && lIt.hasNext()) {
return lIt.next();
}
if (rIt != null && rIt.hasNext()) {
return rIt.next();
}
if (!rootDone) {
rootDone = true;
return root;
}
return null;
}
}
public class LevelOrder extends BinaryTreeIterator {//層次遍歷
Queue<BinaryTree<E>> queue = new ArrayDeque<BinaryTree<E>>();
public boolean hasNext() {
return (!rootDone || !queue.isEmpty());
}
public Object next() {
if (!rootDone) {
if (left != null) {
queue.add(left);
}
if (right != null) {
queue.add(right);
}
rootDone = true;
return root;
}
if (!queue.isEmpty()) {
BinaryTree<E> tree = queue.remove();
if (tree.left != null) {
queue.add(tree.left);
}
if (tree.right != null) {
queue.add(tree.right);
}
return tree.root;
}
return null;
}
}
}
tree = [A, B, D, G, E, C, F, H, I]
PreOrder Traversal: A B D G E C F H I
InOrder Traversal: D G B E A H F I C
PostOrder Traversal: G D E B H I F C A
LevelOrder Traversal: A B C D E F G H I
五.排序
(1)冒泡排序
private static void swap(int[] a, int i, int j) {
if (i == j) {
return;
}
int temp=a[j];
a[j] = a[i];
a[i] = temp;
}
}
public static void sort(int[] a) {
for (int i = a.length-1; i > 0; i--) { // step 1
for (int j = 0; j < i; j++) { // step 2
if (a[j] > a[j+1]) {
swap(a, j, j+1); // step 3
}
}
}
(2)快速排序
public static void sort(int[] a) {
sort(a, 0, a.length);
}
private static void sort(int[] a, int p, int q) {
if (q - p < 2) {
return;
}
int m = partition(a, p, q); // step 2
sort(a, p, m); // step 4
sort(a, m+1, q); // step 5
private static int partition(int[] a, int p, int q) {
int pivot = a[p], i = p, j = q; // steps 1-2
while (i < j) { // step 3
while (i < j && a[--j] >= pivot) ; // step 4
if (i < j) {
a[i] = a[j]; // step 5
}
while (i < j && a[++i] <= pivot) ; // step 6
if (i < j) {
a[j] = a[i]; // step 7
}
}
a[j] = pivot; // step 8
return j;
}
還有其他排序方式,選擇排序,堆排序。。。