1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// java.util.Vector -- an implementation of the Java Language Spec sec 21.11
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.
package java.util;
class VectorEnumeration implements Enumeration {
private int nextElement=0;
private Object[] elements;
private int elementCount;
VectorEnumeration(Object[] elems, int count)
{
elements=elems;
elementCount=count;
}
public boolean hasMoreElements()
{
return nextElement<elementCount;
}
public Object nextElement() throws NoSuchElementException
{
if (!hasMoreElements())
throw new NoSuchElementException();
return elements[nextElement++];
}
}
public class Vector implements Cloneable {
// SUBCLASS INTERFACE
protected Object[] elementData;
protected int elementCount;
protected int capacityIncrement;
// CONSTRUCTORS
public Vector(int initialCapacity, int capacityIncrement)
{
elementData=new Object[initialCapacity];
this.capacityIncrement=capacityIncrement;
elementCount=0;
}
public Vector(int initialCapacity)
{
this(initialCapacity, 0);
}
public Vector()
{
this(10, 0);
}
// PUBLIC INTERFACE
public final synchronized String toString()
{
StringBuffer output=new StringBuffer();
output.append('[');
for (int i=0; i<elementCount; i++) {
if (i>0) output.append(", ");
output.append(elementData[i]);
}
output.append(']');
return output.toString();
}
public synchronized Object clone()
{
Vector v=new Vector();
v.capacityIncrement=capacityIncrement;
v.elementCount=elementCount;
v.elementData=new Object[elementData.length];
for (int i=0; i<elementData.length; i++) {
v.elementData[i]=elementData[i];
}
return v;
}
public final synchronized Object elementAt(int index)
throws IndexOutOfBoundsException
{
if (index<0 || index>=elementCount)
throw new IndexOutOfBoundsException();
else
return elementData[index];
}
public final synchronized void setElementAt(Object obj, int index)
throws IndexOutOfBoundsException
{
if (index<0 || index>=elementCount)
throw new IndexOutOfBoundsException();
else
elementData[index]=obj;
}
public final synchronized Object firstElement()
throws NoSuchElementException
{
if (elementCount==0)
throw new NoSuchElementException();
else
return elementData[0];
}
public final synchronized Object lastElement()
throws NoSuchElementException
{
if (elementCount==0)
throw new NoSuchElementException();
else
return elementData[elementCount-1];
}
public final synchronized void addElement(Object o)
{
ensureCapacity(elementCount+1);
elementData[elementCount++]=o;
}
public final synchronized void insertElementAt(Object o, int index)
throws IndexOutOfBoundsException
{
if (index<0 || index>elementCount)
throw new IndexOutOfBoundsException();
ensureCapacity(elementCount+1);
for (int i=elementCount-1; i>=index; i++) {
elementData[i+1]=elementData[i];
}
elementData[index]=o;
elementCount++;
}
public final synchronized boolean removeElement(Object o)
{
int i=indexOf(o);
if (i>=0) {
removeElementAt(i);
return true;
} else {
return false;
}
}
public final synchronized void removeElementAt(int index)
throws IndexOutOfBoundsException
{
if (index<0 || index>=elementCount)
throw new IndexOutOfBoundsException();
for (int i=index+1; i<elementCount; i++) {
elementData[i-1]=elementData[i];
}
elementCount--;
}
public final synchronized void removeAllElements()
{
elementCount=0;
}
public final synchronized boolean isEmpty()
{
return elementCount==0;
}
public final synchronized int size()
{
return elementCount;
}
public final synchronized void setSize(int newsize)
{
ensureCapacity(newsize);
for (int i=elementCount; i<newsize; i++) {
elementData[i]=null;
}
elementCount=newsize;
}
public final synchronized int capacity()
{
return elementData.length;
}
// Private helper method for ensureCapacity() and trimToSize()
private void adjustSize(int newcap)
{
Object[] newdata=new Object[newcap];
copyInto(newdata);
elementData=newdata;
}
public final synchronized void ensureCapacity(int mincap)
{
if (mincap<=elementData.length)
return;
int newcap=elementData.length;
if (capacityIncrement==0) {
newcap*=2;
} else {
newcap+=capacityIncrement;
}
if (mincap>newcap) newcap=mincap;
adjustSize(newcap);
}
public final synchronized void trimToSize()
{
adjustSize(elementCount);
}
public final synchronized void copyInto(Object[] anarray)
throws IndexOutOfBoundsException
{
for (int i=0; i<elementCount; i++) {
anarray[i]=elementData[i];
}
}
public final synchronized Enumeration elements()
{
return new VectorEnumeration(elementData, elementCount);
}
public final boolean contains(Object elem)
{
return indexOf(elem, 0)!=-1;
}
public final int indexOf(Object elem)
{
return indexOf(elem, 0);
}
public final synchronized int indexOf(Object elem, int index)
{
for (int i=index; i<elementCount; i++) {
if (elem.equals(elementData[i])) {
return i;
}
}
return -1;
}
public final int lastIndexOf(Object elem)
{
return lastIndexOf(elem, elementCount-1);
}
public final synchronized int lastIndexOf(Object elem, int index)
{
for (int i=index; i>=0; i--) {
if (elem.equals(elementData[i])) {
return i;
}
}
}
}
|