Java Vector

Vector ArrayList

VectorArrayList
()
(50%)
fail-fastfail-fast
Java 1.0Java 1.2

  1. Vector

  2. ArrayList

  3. RandomAccess

Vector

Vector 4

10

Vector()

Vector(int size)

incr

Vector(int size,int incr)

c

Vector(Collection c)

Vector

//
boolean add(E e) //
void add(int index, E element) //
boolean addAll(Collection<? extends E> c) //

//
E get(int index) //
E firstElement() //
E lastElement() //

//
E remove(int index) //
boolean remove(Object o) //
void clear() //

//
int size() //
int capacity() //
void ensureCapacity(int minCapacity) //
void trimToSize() //
boolean contains(Object o) //
int indexOf(Object o) //
1 void add(int index, Object element)
 
2 boolean add(Object o) 
 
3 boolean addAll(Collection c) 
Collection collection
4 boolean addAll(int index, Collection c) 
Collection
5 void addElement(Object obj) 
  1
6 int capacity() 
7 void clear() 
8 Object clone() 
9 boolean contains(Object elem) 
 true
10 boolean containsAll(Collection c) 
Collection true
11 void copyInto(Object[] anArray) 
 
12 Object elementAt(int index) 
13 Enumeration elements() 
14 void ensureCapacity(int minCapacity) 
15 boolean equals(Object o) 
16 Object firstElement() 
 0)
17 Object get(int index) 
18 int hashCode() 
19 int indexOf(Object elem) 
  -1
20 int indexOf(Object elem, int index) 
  index  -1
21 void insertElementAt(Object obj, int index) 
 index 
22 boolean isEmpty() 
23 Object lastElement() 
24 int lastIndexOf(Object elem) 
  -1
25 int lastIndexOf(Object elem, int index) 
 index  -1
26 Object remove(int index) 
 
27 boolean remove(Object o) 
28 boolean removeAll(Collection c) 
Collection
29 void removeAllElements() 
30 boolean removeElement(Object obj) 
31 void removeElementAt(int index) 
32 protected void removeRange(int fromIndex, int toIndex)
List fromIndex toIndex
33 boolean retainAll(Collection c) 
Collection
34 Object set(int index, Object element)
 
35 void setElementAt(Object obj, int index) 
 index 
36 void setSize(int newSize) 
 
37 int size() 
 
38 List subList(int fromIndex, int toIndex) 
List fromIndex toIndex
39 Object[] toArray()
 
40 Object[] toArray(Object[] a) 
41 String toString() 
String
42 void trimToSize() 
 

import java.util.*;

public class VectorDemo {

   public static void main(String args[]) {
      // initial size is 3, increment is 2
      Vector v = new Vector(3, 2);
      System.out.println("Initial size: " + v.size());
      System.out.println("Initial capacity: " +
      v.capacity());
      v.addElement(new Integer(1));
      v.addElement(new Integer(2));
      v.addElement(new Integer(3));
      v.addElement(new Integer(4));
      System.out.println("Capacity after four additions: " +
          v.capacity());

      v.addElement(new Double(5.45));
      System.out.println("Current capacity: " +
      v.capacity());
      v.addElement(new Double(6.08));
      v.addElement(new Integer(7));
      System.out.println("Current capacity: " +
      v.capacity());
      v.addElement(new Float(9.4));
      v.addElement(new Integer(10));
      System.out.println("Current capacity: " +
      v.capacity());
      v.addElement(new Integer(11));
      v.addElement(new Integer(12));
      System.out.println("First element: " +
         (Integer)v.firstElement());
      System.out.println("Last element: " +
         (Integer)v.lastElement());
      if(v.contains(new Integer(3)))
         System.out.println("Vector contains 3.");
      // enumerate the elements in the vector.
      Enumeration vEnum = v.elements();
      System.out.println("\nElements in vector:");
      while(vEnum.hasMoreElements())
         System.out.print(vEnum.nextElement() + " ");
      System.out.println();
   }
}

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.

Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12

Java [Java ]Java