Java Tutorial/Collections/Vector — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:04, 1 июня 2010

Содержание

Adding Another Collection

public boolean addAll(Collection c)
public boolean addAll(int index, Collection c)



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]


Adding Elements: Adding at the End

Adding the element to the end of the vector.

The difference is that add() will always return true, while addElement() has no return value.



public boolean add(Object element)
public void addElement(Object element)



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Adding in the Middle

public void add(int index, Object element)
public void insertElementAt(Object element, int index)



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]


Append all elements of other Collection to Vector

import java.util.Vector;
import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");
    v.add("2");
    v.add("3");
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("4");
    arrayList.add("5");
    v.addAll(arrayList);
    for (int i = 0; i < v.size(); i++){
      System.out.println(v.get(i));
    }
  }
}





Checking for Collection Containment: containsAll()

public boolean containsAll(Collection c)



true


Checking for Position from End

public int lastIndexOf(Object element)
public int lastIndexOf(Object element, int index)



[A, I, C, D, E, F, G, H, I, J]
8


Checking for Position: where it is in the vector

public int indexOf(Object element)
public int indexOf(Object element, int index)



1


Checking Vectors for Equality: Containing equivalent elements in identical order

public boolean equals(Object o)



false


Converting the vector to an array: public Object[] toArray()

This creates an array of the proper size, fills it with all the elements in the vector.

You need to cast it to the appropriate type.



import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v1 = new Vector();
    v1.add("A");
    v1.add("B");
    v1.add("C");
    Object[] array = v1.toArray();
    for (int i = 0; i < array.length; i++) {
      System.out.println(array[i]);
    }
  }
}



A
B
C


Converting the vector to an array: public Object[] toArray(Object[] a)

If all elements are the same type, it is easier to work with an array of that specific type.



import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v1 = new Vector();
    v1.add("A");
    v1.add("B");
    v1.add("C");
    String array[] = new String[0];
    array = (String[]) v1.toArray(array);
    for (int i = 0; i < array.length; i++) {
      System.out.println(array[i]);
    }
  }
}



A
B
C


Converting the vector to an array: public void copyInto(Object[] anArray)

import java.util.Vector;
import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v1 = new Vector();
    v1.add("A");
    v1.add("B");
    v1.add("C");
    String array[] = new String[v1.size()];
    v1.copyInto(array);
    for (int i = 0; i < array.length; i++) {
      System.out.println(array[i]);
    }
  }
}



A
B
C


Copy Elements of One Vector to Another Vector with Collection.copy

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v1 = new Vector<String>();
    v1.add("1");
    v1.add("2");
    v1.add("3");
    Vector<String> v2 = new Vector<String>();
    v2.add("One");
    v2.add("Two");
    v2.add("Three");
    v2.add("Four");
    v2.add("Five");
    System.out.println(v2);
    Collections.copy(v2, v1);
    System.out.println(v2);
  }
}
/*[One, Two, Three, Four, Five]
[1, 2, 3, Four, Five]
*/





Copy Elements of Vector to ArrayList with Collection.copy

import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");
    v.add("2");
    v.add("3");
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("One");
    arrayList.add("Two");
    arrayList.add("Three");
    arrayList.add("Four");
    arrayList.add("Five");
    System.out.println(arrayList);
    Collections.copy(arrayList, v);
    System.out.println(arrayList);
  }
}
/*
[One, Two, Three, Four, Five]
[1, 2, 3, Four, Five]
*/





Copying and Cloning Vectors

public Object clone()



[A, C, B]
[A, B, C]


Count distinct elements in a Vector

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
  public static void main(String[] argv) {
    List<String> list = new ArrayList<String>();
    list.add("A");
    list.add("2");
    list.add("c2");
    
    System.out.println(countDistinctElements(list, 0, list.size()));
  }
  static int countDistinctElements(List source, int position, int count){
    List list = source.subList(position, position + count);
    Set set = new HashSet();
    set.addAll(list);
    return set.size();
  }
}





Create Java ArrayList From Enumeration which is from Vector

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("A");
    v.add("B");
    v.add("D");
    v.add("E");
    v.add("F");
    System.out.println(v);
    Enumeration e = v.elements();
    ArrayList<String> aList = Collections.list(e);
    System.out.println(aList);
  }
}
/*
[A, B, D, E, F]
[A, B, D, E, F]
*/





ensureCapacity(): make sure a vector is large enough before adding elements: public void ensureCapacity(int minCapacity)

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v.add(0,i);
    }
    System.out.println(v);
    System.out.println(v.size());  
    v.ensureCapacity(40);
    for (int i = 0; i < 10; i++) {
      v.add(0,i);
    }
    
    System.out.println(v);
    System.out.println(v.size());
  }
}



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
10
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
20


Enumerate through a Vector using Java Enumeration

import java.util.Enumeration;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("One");
    v.add("Two");
    v.add("Three");
    v.add("Four");
    Enumeration e = v.elements();
    while (e.hasMoreElements())
      System.out.println(e.nextElement());
  }
}





Enumerating through the Elements

import java.util.Enumeration;
import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector<String> v = new Vector<String>();
    v.add("A");
    v.add("B");
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
      System.out.println(e.nextElement());
    }
  }
}



A
B


Finding all the positions for a single element

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    String members[] = { "A", "I", "C", "D", "E", "F", "G", "H", "I", "J" };
    Object value = "I";
    Vector v = new Vector();
    for (int i = 0, n = members.length; i < n; i++) {
      v.add(members[i]);
    }
    int index = 0;
    int length = v.size();
    while ((index < length) && (index >= 0)) {
      index = v.indexOf(value, index);
      if (index != -1) {
        System.out.println(index);
        index++;
      }
    }
  }
}



1
8


Find maximum element of Java Vector

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<Double> v = new Vector<Double>();
    v.add(new Double("3.4324"));
    v.add(new Double("3.3532"));
    v.add(new Double("3.342"));
    v.add(new Double("3.349"));
    v.add(new Double("2.3"));
    Object obj = Collections.max(v);
    System.out.println(obj);
  }
}
//3.4324





Find Minimum element of Java Vector

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<Double> v = new Vector<Double>();
    v.add(new Double("3.4"));
    v.add(new Double("3.3"));
    v.add(new Double("4.4"));
    v.add(new Double("5.3"));
    v.add(new Double("3.3"));
    Object obj = Collections.min(v);
    System.out.println(obj);
  }
}





Get Enumeration over Java Vector

import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("A");
    v.add("B");
    v.add("D");
    v.add("E");
    v.add("F");
    Enumeration e = Collections.enumeration(v);
    while (e.hasMoreElements())
      System.out.println(e.nextElement());
  }
}





Getting by Position

The firstElement() and lastElement(): get the element at position 0 and also at the end (position size-1).



public Object firstElement()
public Object lastElement()



A
B


Getting elements by Index with Generics

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector<String> v = new Vector<String>();
    v.add("Hello");
    v.add("Hello");
    String s = v.get(0);
    System.out.println(s);
  }
}



Hello


Getting elements by Index without generics

public Object get(int index)
public Object elementAt(int index)
(starting from zero)



Hello


Java vectors: dynamically sized arrays with synchronized access.

Creating Vectors



public Vector()
public Vector(int initialCapacity)
public Vector(int initialCapacity, int capacityIncrement)



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Multidimensional Vectors (Vectors of Vectors)

MyType o = (MyType)((Vector)vector.elementAt(3)).elementAt(2);



A


Passing the object to remove to either of the remove() or removeElement() methods

public boolean remove(Object element)
public boolean removeElement(Object element)



10
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[8, 7, 6, 5, 4, 3, 1, 0]
10


Perform Binary Search on Java Vector

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("Z");
    v.add("B");
    v.add("D");
    v.add("E");
    v.add("F");
    Collections.sort(v);
    System.out.println(v);
    int index = Collections.binarySearch(v, "D");
    System.out.println("Element found at : " + index);
  }
}
/*[B, D, E, F, Z]
Element found at : 1
*/





Printing Vectors: a comma-delimited list, in index order and surrounded by square brackets ([])

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v.insertElementAt(i,0);
    }
    System.out.println(v);
  }
}



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]


Removing All Elements: clear out all of a vector"s elements: clear() and removeAllElements()

public void clear()
public void removeAllElements()



[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[]


Removing Another Collection: public boolean removeAll(Collection c)

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v.add(0,i);
    }
    System.out.println(v);
  
    Vector v2 = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v2.add(0,i);
    }
    System.out.println(v2);
    
    System.out.println(v2.equals(v));
    
    v2.removeAll(v);
    
    System.out.println(v2);
  }
  
}



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
true
[]


Removing Single Elements

public Object remove(int index)
public void removeElementAt(int index)



10
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 7, 5, 4, 3, 2, 1, 0]
10


Replace All Elements Of Vector with Collections.fill

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("A");
    v.add("B");
    v.add("D");
    System.out.println(v);
    Collections.fill(v, "REPLACED");
    System.out.println(v);
  }
}
/*
[A, B, D]
[REPLACED, REPLACED, REPLACED]
*/





Replace all occurrences of specified element of Java Vector

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("A");
    v.add("B");
    v.add("A");
    v.add("C");
    v.add("D");
    System.out.println(v);
    Collections.replaceAll(v, "A", "Replace All");
    System.out.println(v);
  }
}





Replace an element at specified index of Java Vector

import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");
    v.add("2");
    v.add("3");
    
    v.set(1, "REPLACED ELEMENT");
    for (String str : v) {
      System.out.println(str);
    }
  }
}





Replacing Elements

set() and setElementAt(): replace individual elements in an array



public Object set(int index, Object element)
public void setElementAt(Object obj, int index)



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[A, A, A, A, A, A, A, A, A, A]


Retaining Another Collection: public boolean retainAll(Collection c)

Only those elements within the collection argument are kept within the vector. Everything else is removed instead.



import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v.add(0,i);
    }
    System.out.println(v);
  
    Vector v2 = new Vector(5);
    for (int i = 0; i < 5; i++) {
      v2.add(0,i);
    }
    System.out.println(v2);
    System.out.println(v2.equals(v));
    
    v.retainAll(v2);
    System.out.println(v);
  }
}



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[4, 3, 2, 1, 0]
false
[4, 3, 2, 1, 0]


Reverse order of all elements of Java Vector

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("A");
    v.add("B");
    v.add("C");
    v.add("D");
    v.add("E");
    System.out.println(v);
    Collections.reverse(v);
    System.out.println(v);
  }
}
/*
[A, B, C, D, E]
[E, D, C, B, A]
*/





Search an element of Java Vector

import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("5");
    v.add("1");
    v.add("2");
    System.out.println(v.contains("3"));
    System.out.println(v.indexOf("5"));
    System.out.println(v.lastIndexOf("2"));
  }
}





Search an element of Vector from specific index

import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("5");
    v.add("1");
    v.add("2");
    System.out.println(v.indexOf("1", 4));
    System.out.println(v.lastIndexOf("2", 5));
  }
}





Serializing Vector

A vector is only serializable if all the elements are also serializable. Or, you"ll get a NotSerializableException thrown.



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;
public class MainClass {
  public static void main (String args[]) throws Exception {
    Vector v = new Vector(Arrays.asList("a","b","c"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(v);
    oos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    Vector v2 = (Vector)ois.readObject();
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
      System.out.println(e.nextElement());
    }
  }
}



a
b
c


Setting vector"s size to trim elements

  1. If you make the size smaller than the current number of elements, the vector drops elements from the end.
  2. Increasing the size adds null elements to the end.
  3. Calling setSize(0) removes all elements from the vector.
  4. If you set the size to zero or have no elements in the vector, the isEmpty() method returns true:



public boolean isEmpty()



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[]
true


Shuffle elements of Vector

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("5");
    System.out.println(v);
    Collections.shuffle(v);
    System.out.println(v);
  }
}
/*[1, 2, 3, 4, 5]
[5, 4, 1, 3, 2]
*/





Sizing Vectors

The length is like the length of an array and is known as the size.

It represents the number of elements stored in the vector.

You retrieve this setting with the size() method and change it with the setSize() method.



public int size()
public void setSize(int newSize)



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7]


Sort Vector in descending order using comparator

import java.util.Collections;
import java.util.ruparator;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("5");
    Comparator comparator = Collections.reverseOrder();
    System.out.println(v);
    Collections.sort(v, comparator);
    System.out.println(v);
  }
}





Storage Capacity

  1. Capacity is the number of elements a vector can hold before resizing its internal data structures.
  2. For performance reasons, it"s best to reduce the number of times the internal structure has to resize.
  3. If the capacity is too large, memory goes to waste.
  4. To find out the current capacity of a vector: the capacity() method:



import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v.add(0,i);
    }
    System.out.println(v);
    System.out.println(v.size());  
    for (int i = 0; i < 10; i++) {
      v.add(0,i);
    }
    
    System.out.println(v);
    System.out.println(v.size());
  }
}



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
10
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
20


subList(): taking a subset of the vector"s elements and referencing them from another List

public List subList(int fromIndex, int toIndex)



B


Swap elements of Vector

import java.util.Collections;
import java.util.Vector;
public class Main {
  public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("5");
    System.out.println(v);
    Collections.swap(v, 0, 4);
    System.out.println(v);
  }
}





The asList() method of the Arrays class will create an object

import java.util.Arrays;
import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Integer[] array = {1,2,3,4,5};
    Vector<Integer> v = new Vector<Integer>(Arrays.asList(array));
    
    System.out.println(v);
  }
}



[0, 1, 2, 3, 4, 5]


The contains() method: reports if a specific element is within the vector

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector();
    v.add("A");
    v.add("B");
    boolean isContaining = v.contains("B");
    System.out.println(isContaining);
  }
}



true


The equals() method is used to check for element equality

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v.add(0,i);
    }
    System.out.println(v);
  
    Vector v2 = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v2.add(0,i);
    }
    System.out.println(v2);
    
    System.out.println(v2.equals(v));
  }
  
}



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
true


The Vector class overrides the hashCode() method: public int hashCode()

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v1 = new Vector();
    v1.add("A");
    v1.add("B");
    v1.add("C");
    System.out.println(v1.hashCode());
  }
}



94369


The Vector"s subList() method does not make a clone of the element references

If you replace the element within the sublist, it is replaced in the original vector.

If you remove something from the sublist, it is also removed from the vector.



import java.util.List;
import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v1 = new Vector();
    v1.add("A");
    v1.add("B");
    v1.add("C");
    List l = v1.subList(1, 2);
    l.remove(0);
    System.out.println(l);
    System.out.println(v1);
  }
}



[]
[A, C]


Unmodifiable Vector Adapter

/*
 * $Id: UnmodifiableVectorAdapter.java,v 1.1.1.1 2005/04/07 18:36:25 pocho Exp $
 */
import java.util.Collection;
import java.util.List;
import java.util.Vector;
/**
 * Adapter that adapts any {@link java.util.List} to {@link java.util.Vector} class.
 * 
 * @version $Name:  $ - $Revision: 1.1.1.1 $ - $Date: 2005/04/07 18:36:25 $
 */
public class UnmodifiableVectorAdapter extends Vector {
  
  private List adaptedList;
  
  public UnmodifiableVectorAdapter(List list) {
    setAdaptedList(list);
  }
  
  public void setAdaptedList(List list) {
    this.adaptedList = list;
  }
  
  public boolean contains(Object elem) {
    return adaptedList.contains(elem);
  }
  
  public boolean containsAll(Collection c) {
    return adaptedList.containsAll(c);
  }
  
  public Object elementAt(int index) {
    return adaptedList.get(index);
  }
  
  public Object get(int index) {
    return adaptedList.get(index);
  }
  
  public int indexOf(Object elem) {
    return adaptedList.indexOf(elem);
  }
  
  public int indexOf(Object elem, int index) {
    return subList(index, size()).indexOf(elem);
  }
  public boolean isEmpty() {
    return adaptedList.isEmpty();    
  }
  
  public Object lastElement() {
    return adaptedList.get(size() - 1);
  }
  
  public int lastIndexOf(Object elem) {
    return adaptedList.lastIndexOf(elem);
  }
  
  public int lastIndexOf(Object elem, int index) {
    return subList(0, index).indexOf(elem);
  }
  
  public int size() {
    return adaptedList.size();
  }
  
  public List subList(int fromIndex, int toIndex) {
    return adaptedList.subList(fromIndex, toIndex);
  }
  public Object[] toArray() {
    return adaptedList.toArray();
  }
  
  public Object[] toArray(Object[] a) {
    return adaptedList.toArray(a);
  }
  public String toString() {
    return adaptedList.toString();
  }
  
  public void add(int index, Object element) { 
    throw new UnsupportedOperationException();
  }
  
  
  public boolean add(Object o) {
    throw new UnsupportedOperationException();
  }
  
  public boolean addAll(Collection c) {
    throw new UnsupportedOperationException();
  }
  
  public boolean addAll(int index, Collection c) {
    throw new UnsupportedOperationException(); 
  }
  
  public void addElement(Object obj) {
    throw new UnsupportedOperationException();
  }
  
  public void clear() { 
    throw new UnsupportedOperationException();
  }
  
  public Object clone() {
    throw new UnsupportedOperationException();
  }
  public void insertElementAt(Object obj, int index) {
    throw new UnsupportedOperationException();
  }
  public Object remove(int index) {
    throw new UnsupportedOperationException();
  }
  
  public boolean remove(Object o) {
    throw new UnsupportedOperationException();
  }
  
  public boolean removeAll(Collection c) {
    throw new UnsupportedOperationException();
  }
  
  public void removeAllElements() {
    throw new UnsupportedOperationException();
  }
  
  public boolean removeElement(Object obj) {
    throw new UnsupportedOperationException();
  }
  public void removeElementAt(int index) {
    throw new UnsupportedOperationException();
  }
  public boolean retainAll(Collection c) {
    throw new UnsupportedOperationException();
  }
  
  public Object set(int index, Object element) {
    throw new UnsupportedOperationException();
  }
  
  public void setElementAt(Object obj, int index) {
    throw new UnsupportedOperationException();
  }
  
  
  public void setSize(int newSize) {
    throw new UnsupportedOperationException();
  }
}





Using Iterator to loop through Vector elements

import java.util.Iterator;
import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector<String> v = new Vector<String>();
    v.add("A");
    v.add("B");
    Iterator i = v.iterator();
    while (i.hasNext()) {
      System.out.println(i.next());
    }
  }
}



A
B


Using list Iterator to loop through the vector

public ListIterator listIterator()
public ListIterator listIterator(int index)



A
B


Vector Immutability

import java.util.Collections;
import java.util.List;
import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v.add(0, i);
    }
    System.out.println(v);
    System.out.println(v.size());
    List l = Collections.unmodifiableList(v);
    l.add(1);
    System.out.println(l);
  }
}



[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.Collections$UnmodifiableCollection.add(Collections.java:1018)
	at MainClass.main(MainClass.java:16)


When removing all the elements from a vector, the capacity does not change.

import java.util.Vector;
public class MainClass {
  public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
      v.add(i,0);
    }
    System.out.println(v.capacity());
    System.out.println(v);
    
    v.clear();
    
    System.out.println(v);
    System.out.println(v.capacity());
    
  }
}



10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[]
10