/* 高手之作,本人謹以收藏者身份拿出共用供大家參考! */
/*
* 向量介面
*/
package dsa;
public interface Vector {
//返迴向量中元素數目
public int getSize();
//判斷向量是否為空白
public boolean isEmpty();
//取秩為r的元素
public Object getAtRank(int r)
throws ExceptionBoundaryViolation;
//將秩為r的元素替換為obj
public Object replaceAtRank(int r, Object obj)
throws ExceptionBoundaryViolation;
//插入obj,作為秩為r的元素;返回該元素
public Object insertAtRank(int r, Object obj)
throws ExceptionBoundaryViolation;
//刪除秩為r的元素
public Object removeAtRank(int r)
throws ExceptionBoundaryViolation;
}
/*
* 基於數組的向量實現
*/
package dsa;
public class Vector_Array implements Vector {
private final int N = 1024;//數組的容量
private int n = 0;//向量的實際規模
private Object[] A;//對象數組
//建構函式
public Vector_Array() {
A = new Object[N];
n = 0;
}
//返迴向量中元素數目
public int getSize() { return n; }
//判斷向量是否為空白
public boolean isEmpty() { return (0 == n) ? true : false; }
//取秩為r的元素
public Object getAtRank(int r)//O(1)
throws ExceptionBoundaryViolation {
if (0 > r || r >= n) throw new ExceptionBoundaryViolation("意外:秩越界");
return A[r];
}
//將秩為r的元素替換為obj
public Object replaceAtRank(int r, Object obj)
throws ExceptionBoundaryViolation {
if (0 > r || r >= n) throw new ExceptionBoundaryViolation("意外:秩越界");
Object bak = A[r];
A[r] = obj;
return bak;
}
//插入obj,作為秩為r的元素;返回該元素
public Object insertAtRank(int r, Object obj)
throws ExceptionBoundaryViolation {
if (0 > r || r > n) throw new ExceptionBoundaryViolation("意外:秩越界");
if (n >= N) throw new ExceptionBoundaryViolation("意外:數組溢出");
for (int i=n; i>r; i--) A[i] = A[i-1];//後續元素順次後移
A[r] = obj;//插入
n++;//更新當前規模
return obj;
}
//刪除秩為r的元素
public Object removeAtRank(int r)
throws ExceptionBoundaryViolation {
if (0 > r || r >= n) throw new ExceptionBoundaryViolation("意外:秩越界");
Object bak = A[r];
for (int i=r; i<n; i++) A[i] = A[i+1];//後續元素順次前移
n--;//更新當前規模
return bak;
}
}
/*
* 基於可擴充數組的向量實現
*/
package dsa;
public class Vector_ExtArray implements Vector {
private int N = 8;//數組的容量,可不斷增加
private int n;//向量的實際規模
private Object A[];//對象數組
//建構函式
public Vector_ExtArray() { A = new Object[N]; n = 0; }
//返迴向量中元素數目
public int getSize() { return n; }
//判斷向量是否為空白
public boolean isEmpty() { return (0 == n) ? true : false; }
//取秩為r的元素
public Object getAtRank(int r)
throws ExceptionBoundaryViolation {
if (0 > r || r >= n) throw new ExceptionBoundaryViolation("意外:秩越界");
return A[r];
}
//將秩為r的元素替換為obj
public Object replaceAtRank(int r, Object obj)
throws ExceptionBoundaryViolation {
if (0 > r || r >= n) throw new ExceptionBoundaryViolation("意外:秩越界");
Object bak = A[r];
A[r] = obj;
return bak;
}
//插入obj,作為秩為r的元素;並返回該元素
public Object insertAtRank(int r, Object obj)
throws ExceptionBoundaryViolation {
if (0 > r || r > n) throw new ExceptionBoundaryViolation("意外:秩越界");
if (N <= n) {//空間溢出的處理
N *= 2;
Object B[] = new Object[N];//開闢一個容量加倍的數組
for (int i=0; i<n; i++) B[i] = A[i];//A[]中內容複寫至B[]
A = B;//用B替換A(原A[]將被自動回收)
}
for (int i=n; i>r; i--) A[i] = A[i-1];//後續元素順次後移
A[r] = obj;//插入
n++;//更新當前規模
return obj;
}
//刪除秩為r的元素
public Object removeAtRank(int r)
throws ExceptionBoundaryViolation {
if (0 > r || r >= n) throw new ExceptionBoundaryViolation("意外:秩越界");
Object bak = A[r];
for (int i=r; i<n-1; i++) A[i] = A[i+1];//後續元素順次前移
n--;//更新當前規模
return bak;
}
}