想写一个比较底层的数据结构,在预分配空间不足时会自动增加空间,用数组实现public class VectorArray<T> implements Vector<T> {
private int increment;
private int capacity;
private int Size = 0;
private T[] s; VectorArray(int capacity,int increment) {
s = (T[]) new Object[this.capacity = capacity];
this.increment=increment;
}
}要求当s空间满的时候,重新分配s数组的大小,并保持s原来数组中的对象。不要使用java本身提供的函数,我就是想看下java本身是怎么实现的,java的源代码没有看懂。
谢谢各位达人了哈。
private int increment;
private int capacity;
private int Size = 0;
private T[] s; VectorArray(int capacity,int increment) {
s = (T[]) new Object[this.capacity = capacity];
this.increment=increment;
}
}要求当s空间满的时候,重新分配s数组的大小,并保持s原来数组中的对象。不要使用java本身提供的函数,我就是想看下java本身是怎么实现的,java的源代码没有看懂。
谢谢各位达人了哈。
我只能想到这种办法。
我记的好像ArrayList也是这么做的吧?
不过这样的会涉及整个数组的拷贝,如果数组很大而数组容量不够又频繁发生的话,感觉T(n)就会很大,不知道有没有更优化的方法。