背景:假定集合 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
原文地址:http://blog.csdn.net/github_27609763/article/details/46556995