1 import java.util.NoSuchElementException;
3 public class Vector implements Set {
14 public Vector(int size) {
17 array=new Object[size];
20 public boolean isEmpty() {
29 public int indexOf(Object elem) {
30 return indexOf(elem, 0);
33 public int indexOf(Object elem, int index) {
34 for(int i=index; i<size; i++) {
35 if (elem.equals(array[i]))
41 public boolean contains(Object e) {
42 return indexOf(e)!=-1;
45 /*public boolean remove(Object o) {
55 public Object remove(int index) {
59 removeElementAt(index);
64 public Object elementAt(int index) {
65 if (index<0 | index >=size) {
66 System.printString("Illegal Vector.elementAt\n");
73 public void setElementAt(Object obj, int index) {
77 System.printString("Illegal Vector.setElementAt\n");
82 private void ensureCapacity(int minCapacity) {
83 if (minCapacity>array.length) {
85 if (capacityIncrement<=0)
86 newsize=array.length*2;
88 newsize=array.length+capacityIncrement;
89 if (newsize<minCapacity)
91 Object [] newarray=new Object[newsize];
92 for(int i=0; i<size; i++)
102 public Enumeration elements() {
103 System.printString("Vector.elements not implemented\n");
107 public void addElement(Object obj) {
108 if (size==array.length) {
109 ensureCapacity(size+1);
114 public boolean add(Object obj) {
115 if (size==array.length) {
116 ensureCapacity(size+1);
122 public void insertElementAt(Object obj, int index) {
123 if (index<0||index>size) {
124 System.printString("Illegal Vector.insertElementAt\n");
128 if (size==array.length) {
129 ensureCapacity(size+1);
132 for(int i=size-1; i>index; --i) {
133 array[i] = array[i-1];
138 public void removeElementAt(int index) {
139 if (index<0||index>=size) {
140 System.printString("Illegal Vector.removeElementAt\n");
143 removeElement(array, index, size);
148 public static native void removeElement(Object[] array, int index, int size);
150 public void removeAllElements() {
152 for(int i = 0; i<s; ++i ) {
157 public Object[] toArray() {
158 Object[] tarray = new Object[this.size];
159 for(int i = 0; i < this.size; i++) {
160 tarray[i] = this.array[i];
165 public Vector(Set s) {
166 Object[] sarray = s.toArray();
168 this.size=sarray.length;
172 public synchronized Object firstElement() {
174 throw new /*NoSuchElement*/Exception("NoSuchElement");