Java基於可擴充數組的向量實現(演算法源碼)

來源:互聯網
上載者:User

/* 高手之作,本人謹以收藏者身份拿出共用供大家參考! */

 

/*
 * 向量介面
 */

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;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.