KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > Vector


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package java.util;
17
18 /**
19  * To keep performance characteristics in line with Java community expectations,
20  * <code>Vector</code> is a wrapper around <code>ArrayList</code>.
21  *
22  */

23 public class Vector extends AbstractList JavaDoc implements List JavaDoc, RandomAccess JavaDoc,
24     Cloneable JavaDoc {
25
26   private transient ArrayList JavaDoc arrayList;
27
28   public Vector() {
29     arrayList = new ArrayList JavaDoc();
30   }
31
32   public Vector(Collection JavaDoc c) {
33     arrayList = new ArrayList JavaDoc();
34     addAll(c);
35   }
36
37  /**
38    * There is no speed advantage to pre-allocating array sizes in JavaScript,
39    * so the <code>intialCapacity</code> parameter is ignored. This constructor is
40    * only present for compatibility with JDK 1.4's API.
41    */

42   public Vector(int initialCapacity) {
43     arrayList = new ArrayList JavaDoc(initialCapacity);
44   }
45
46   public void add(int index, Object JavaDoc o) {
47     arrayList.add(index, o);
48   }
49
50   public boolean add(Object JavaDoc o) {
51     return arrayList.add(o);
52   }
53
54   public boolean addAll(Collection JavaDoc c) {
55     return arrayList.addAll(c);
56   }
57
58   public boolean addAll(int index, Collection JavaDoc c) {
59     return arrayList.addAll(index, c);
60   }
61
62   public void addElement(Object JavaDoc o) {
63     add(o);
64   }
65
66   public void clear() {
67     arrayList.clear();
68   }
69
70   public Object JavaDoc clone() {
71     return new Vector JavaDoc(this);
72   }
73
74   public boolean contains(Object JavaDoc elem) {
75     return arrayList.contains(elem);
76   }
77
78   public void copyInto(Object JavaDoc[] objs) {
79     int i = -1;
80     int n = size();
81     while (++i < n) {
82       objs[i] = get(i);
83     }
84   }
85   
86   public Object JavaDoc elementAt(int index) {
87     return get(index);
88   }
89
90   public Object JavaDoc firstElement() {
91     return get(0);
92   }
93
94   public Object JavaDoc get(int index) {
95     return arrayList.get(index);
96   }
97
98   public int indexOf(Object JavaDoc elem) {
99     return arrayList.indexOf(elem);
100   }
101
102   public int indexOf(Object JavaDoc elem, int index) {
103     return arrayList.indexOf(elem, index);
104   }
105
106   public void insertElementAt(Object JavaDoc o, int index) {
107     add(index, o);
108   }
109
110   public boolean isEmpty() {
111     return (arrayList.size() == 0);
112   }
113
114   public Iterator JavaDoc iterator() {
115     return arrayList.iterator();
116   }
117
118   public Object JavaDoc lastElement() {
119     if (isEmpty()) {
120       throw new IndexOutOfBoundsException JavaDoc("last");
121     } else {
122       return get(size() - 1);
123     }
124   }
125
126   public int lastIndexOf(Object JavaDoc o) {
127     return arrayList.lastIndexOf(o);
128   }
129
130   public int lastIndexOf(Object JavaDoc o, int index) {
131     return arrayList.lastIndexOf(o, index);
132   }
133
134   public Object JavaDoc remove(int index) {
135     return arrayList.remove(index);
136   }
137
138   public void removeAllElements() {
139     clear();
140   }
141
142   public boolean removeElement(Object JavaDoc o) {
143     return remove(o);
144   }
145
146   public void removeElementAt(int index) {
147     remove(index);
148   }
149
150   public Object JavaDoc set(int index, Object JavaDoc elem) {
151     return arrayList.set(index, elem);
152   }
153
154   public void setElementAt(Object JavaDoc o, int index) {
155     set(index, o);
156   }
157
158   public void setSize(int size) {
159     arrayList.setSize(size);
160   }
161
162   public int size() {
163     return arrayList.size();
164   }
165
166   public Object JavaDoc[] toArray() {
167     return arrayList.toArray();
168   }
169
170   protected void removeRange(int fromIndex, int endIndex) {
171     arrayList.removeRange(fromIndex, endIndex);
172   }
173 }
174
Popular Tags