从头开始构建Java矢量

时间:2014-04-04 15:36:36

标签: java arrays vector jvm

长话短说,我在大学Comp Sci课上,教授希望我们从头开始构建一个Vector。他们在网上有什么好的教程吗?我做了谷歌搜索但没有真正出现过。

修改

根据文件老师发出的载体需要能够做到以下几点:

  • append(Object element) - 在向量的末尾附加元素
  • clear() - 使矢量集合为空
  • contains(Object element) - 检查向量是否包含元素
  • elementAt(int index) - 访问给定索引处的元素
  • indexOf(Object element) - 查找元素的索引
  • insertAt(int index,Object element) - 在给定索引处插入元素
  • isEmpty() - 检查向量是否为空
  • removeAt(int index) - 删除给定索引处的元素
  • remove(Object element) - 从向量中删除元素
  • replace(int index,Object element) - 将给定索引处的元素替换为给定元素
  • size() - 获取当前向量中的元素数量
  • ensureCapacity(int minCapacity) - 确保向量至少达到给定容量
  • clone() - 返回此向量的克隆副本
  • removeRange(int fromIndex,int toIndex) - 从此向量中删除索引介于fromIndex(包含)和toIndex(独占)之间的所有元素
  • toString() - 返回此向量的字符串表示形式,包含每个元素的String表示形式
  • reverse() - 反转此向量中的元素
  • merge(MyVector vector2) - 将vector2中的所有元素添加到此向量的末尾

此外,该课程需要克隆Cloneable并自我扩展。

这是我到目前为止所提出的:

package collection;

public class MyVector implements Cloneable {

    protected Object[] items;
    protected int arraySize;
    protected int maxCap;

    public MyVector (int initialCapacity) {
        super();
        if (initialCapacity < 0) {
            throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
        }
        this.items = new Object[initialCapacity];
        this.arraySize = 0;
        this.maxCap = initialCapacity;
    }

    public MyVector() {
        this(10);
    }

    public void append(Object element) {
        int newArraySize = this.arraySize + 1;
        if(this.maxCap == newArraySize) {
            this.items = this.increaseCap(newArraySize);
            this.items[this.arraySize] = element;
            this.arraySize += 1;
            //System.out.println(this.items[this.arraySize);
        } else {
            this.items[this.arraySize] = element;
            this.arraySize +=1;
        }
    }
    @Override
    public String toString() {
        String output = "[";
        //output = this.items[0].toString();
        for(int i = 0; i < this.arraySize; i++) {
            output += this.items[i] + ", ";
        }
        output += "]";
        return output;
    }
    public void clear() {
        for(int i = 0; i < this.arraySize; i++) {
            this.items[i] = null;
            this.arraySize = 0;
        }
    }
    public boolean contains(Object element) {
        boolean doesContain = false;
        for(int i = 0; i < this.arraySize; i++) {
            if(element == this.items[i]) {
                doesContain = true;
                i = this.arraySize;
            }
        }
        return doesContain;
    }
    public Object elementAt(int index) {
        if(this.arraySize >= index) {
            return this.items[index];
        } else {
            Object temp = null;
            System.out.println("No index of " + index);
            return temp; 
        }
    }
    public Object indexOf(Object element) {
        Object index = "No value found";
        for(int i = 0; i < this.arraySize; i++) {
            if(element == this.items[i]) {
                index = i;
                break;        
            } 
        }
        return index;
    }
    public boolean isEmpty() {
        if(this.arraySize == 0) {
            return true;
        }
        return false;
    }
    public void replace(int index, Object element) {
        if(this.arraySize > index) {
            this.items[index] = element;
        } else {
            System.out.println("No element at " + index);
        }
    }
    public int size() {
        return this.arraySize;
    }
    public void reverse() {
        Object[] temp = new Object[this.items.length];
        int j = this.arraySize;
        for(int i = 0; i < this.arraySize; i++) {
            temp[j] = this.items[i];
            j--;
        }
        this.items = temp;
    }
    public void ensureCapacity(int minCapacity) {
        if(minCapacity > this.items.length) {
            this.items = this.increaseCap(minCapacity);
        }
    }
    public Object[] increaseCap(int minCap) {
        Object[] arr1 = new Object[minCap * 2];
        for(int i = 0; i < minCap; i++) {
            arr1[i] = this.items[i];
        }
        this.maxCap = this.maxCap * 2;
        return arr1;
    }
    @Override
    public Object clone() {
        return this.items;
    }
    public boolean checkIndex(int index) {
        boolean check = false;
        if(index < this.arraySize) {
            check = true;
        }
        return check;
    }
    public void removeAt(int index) {
        if(true == this.checkIndex(index)) {
            Object[] temp = new Object[this.arraySize - 1];
            for(int j = 0; j < index; j++) {
                temp[j] = this.items[j];
            }
            for(int j = index + 1; j < this.arraySize; j++) {
                temp[j-1] = this.items[j];
            }
            this.items = temp;
            this.arraySize = this.arraySize - 1;
        }
    }
    public void insertAt(int index, Object element) {
        if (this.checkIndex(index) == true) {
            Object[] temp = new Object[this.arraySize];
            for(int i = index; i < this.arraySize; i++) {
                temp[i+1] = this.items[i];
            }
            this.items[index] = element;
            for (int i = index + 1; i < this.arraySize; i++) {
                this.items[i] = temp[i - 1];
            }
            this.arraySize = this.arraySize - 1;
        }
    }
    public void remove(Object element) {
        for(int i = 0; i < this.items.length; i++) {
            if(this.items[i] == element) {
                this.removeAt(i);
            }
        }
    }
    public void removeRange(int fromIndex, int toIndex) {
        for(int i = fromIndex; i < toIndex; i++) {
            this.removeAt(i);
        }
    }
    public void merge(MyVector vector2) {
        this.ensureCapacity(vector2.size() + this.arraySize);
        for(int i = 0; i < vector2.size(); i++) {
            this.append(vector2);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

假设您的作业正在复制java.util.Vector,我会看一下Vector是什么来复制它:

  

向量实现 动态数组 。它类似于 ArrayList ,但是   有两点不同:

     
      
  1. Vector 已同步

  2.   
  3. Vector包含许多不属于集合框架的 遗留方法

  4.   

可以尝试以同步的方式使用ArrayList来复制Vector,但我确信有更好的解决方案。