背景:假定集合 S 由 n 個元素組成,它們按照線性次序存放,於是我們就可以直接訪問其中的第一個元素、第二個元素、第三個元素……。也就是說,通過[0, n-1]之間的每一個整數,都可以直接訪問到唯一的元素 e,而這個整數就等於 S 中位於 e 之前的元素個數⎯⎯在此,我們稱之爲該元素的秩( Rank)。不難看出,若元素 e 的秩爲 r,則只要 e 的直接前驅(或直接後繼)存在,其秩就是 r-1(或 r+1)。這一定義與 Java、 C++之類的程序語言中關於數組元素的編號規則是一致的。支持通過秩直接訪問其中元素的序列,稱作向量( Vector)或數組列表( Array list)。實際上,秩這一直觀概念的功能非常強大,它可以直接指定插入或刪除元素的位置。
向量ADT各方法定義如下:
基於數組,可以直接實現向量 ADT。我們借用一個數組 array[],其中 array[i]分別存放一個引用,指向秩爲position 的向量元素。爲此, array[]的容量 CAPACITY 需要足夠大,還需要設置一個實例變量 size 指示向量的實際規模。
具體代碼實現:
Vector 接口:
/**
* Vector Interface
*/
package com.vector_and_array;
/**
* @author gannyee
*
*/
public interface VectorInterface {
//Get size of vector
public int getSize();
//Is empty
public boolean isEmpty();
//Get element at rank
public Object getAtRank(int position) throws ExceptionBoundaryExceed;
//Replace element at rank
public void replaceAtRank(int position,Object element) throws ExceptionBoundaryExceed;
//Insert element at rank
public void insertAtRank(int position,Object element) throws ExceptionBoundaryExceed;
//Remove element at rank
public void removeAtRank(int position) throws ExceptionBoundaryExceed;
//Travel all elements
public void getAllelements();
}
自定義ExceptionBoundaryExceed類:
package com.vector_and_array;
/**
* @author gannyee
* Exception for boundaryExceed
*/
public class ExceptionBoundaryExceed extends Exception{
//Constructor overload
public ExceptionBoundaryExceed(String message){
super(message);
}
}
Vector具體實現
VectorBasicOnArray類:
package com.vector_and_array;
import java.util.Arrays;
public class VectorBasicOnArrays implements VectorInterface {
//Define static variable
private static final int CAPACITY = 1024;
//Declared size of vector
private static int size;
//Declared array for vector
Object[] array;
//Constructor
public VectorBasicOnArrays(){
size = 0;
array = new Object[CAPACITY];
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public Object getAtRank(int position) throws ExceptionBoundaryExceed{
if(position < 0 || position > size)
throw new ExceptionBoundaryExceed("Vector exceed! ");
return array[position];
}
@Override
public void replaceAtRank(int position, Object element) throws ExceptionBoundaryExceed{
if(position < 0 || position > size)
throw new ExceptionBoundaryExceed("Vector exceed! ");
array[position] = element;
System.out.println("Element replaced is: " + array[position] + " at position: " + position);
}
@Override
public void insertAtRank(int position, Object element) throws ExceptionBoundaryExceed{
if(position < 0 || position > size)
throw new ExceptionBoundaryExceed("Vector exceed! ");
if(size > CAPACITY)
throw new ExceptionBoundaryExceed("Vector overflow! ");
for(int i = size;i >= position;i --)
array[i + 1] = array[i];
array[position] = element;
size ++;
System.out.println("Element you insert is: " + element + " at position: " + position);
}
@Override
public void removeAtRank(int position) throws ExceptionBoundaryExceed{
if(position < 0 || position > size)
throw new ExceptionBoundaryExceed("Vector exceed! ");
System.out.println("Element you remove is: " + array[position]);
for(int i = position;i <= size - 1;i ++)
array[i] = array[i + 1];
size --;
}
@Override
public void getAllelements() {
Object[] arrayTravel = new Object[size];
for(int i = 0;i < arrayTravel.length;i ++)
arrayTravel[i] = array[i];
System.out.println("All elements are: " + Arrays.toString(arrayTravel));
}
}
測試代碼:
package com.vector_and_array;
/**
* Test
* @author gannyee
*
*/
public class VectorTest {
public static void main(String[] args) throws ExceptionBoundaryExceed {
//Define VectorBasicOnArrat class
VectorBasicOnArrays vba = new VectorBasicOnArrays();
System.out.println("Size: " + vba.getSize());
System.out.println("Is empty? " + vba.isEmpty());
vba.insertAtRank(0, 1);
vba.insertAtRank(1, 2);
vba.insertAtRank(2, 3);
vba.insertAtRank(3, 4);
vba.insertAtRank(4, 5);
vba.insertAtRank(5, 6);
vba.insertAtRank(5, 7);
vba.insertAtRank(6, 8);
System.out.println("Size: " + vba.getSize());
System.out.println("Is empty? " + vba.isEmpty());
vba.getAllelements();
vba.replaceAtRank(0, 12);
vba.replaceAtRank(1, 13);
vba.replaceAtRank(2, 14);
vba.replaceAtRank(3, 15);
System.out.println("Size: " + vba.getSize());
System.out.println("Is empty? " + vba.isEmpty());
vba.getAllelements();
vba.removeAtRank(7);
vba.removeAtRank(5);
vba.removeAtRank(4);
vba.removeAtRank(1);
vba.removeAtRank(0);
System.out.println("Size: " + vba.getSize());
System.out.println("Is empty? " + vba.isEmpty());
vba.getAllelements();
}
}
測試結果:
Size: 0
Is empty? true
Element you insert is: 1 at position: 0
Element you insert is: 2 at position: 1
Element you insert is: 3 at position: 2
Element you insert is: 4 at position: 3
Element you insert is: 5 at position: 4
Element you insert is: 6 at position: 5
Element you insert is: 7 at position: 5
Element you insert is: 8 at position: 6
Size: 8
Is empty? false
All elements are: [1, 2, 3, 4, 5, 7, 8, 6]
Element replaced is: 12 at position: 0
Element replaced is: 13 at position: 1
Element replaced is: 14 at position: 2
Element replaced is: 15 at position: 3
Size: 8
Is empty? false
All elements are: [12, 13, 14, 15, 5, 7, 8, 6]
Element you remove is: 6
Element you remove is: 7
Element you remove is: 5
Element you remove is: 13
Element you remove is: 12
Size: 3
Is empty? false
All elements are: [14, 15, 8]
相關文字借鑑引用 數據結構與算法( Java 描述)鄧俊輝 著
未完待續。。。。
基於數組自定義實現容量可變的向量Vector!
源碼在github上:https://github.com/gannyee/JavaDataStruct