Java Tutorial/Collections/Vector

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

Содержание

Adding Another Collection

   <source lang="java">

public boolean addAll(Collection c) public boolean addAll(int index, Collection c)</source>



[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.



   <source lang="java">

public boolean add(Object element) public void addElement(Object element)</source>



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


Adding in the Middle

   <source lang="java">

public void add(int index, Object element) public void insertElementAt(Object element, int index)</source>



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


Append all elements of other Collection to Vector

   <source lang="java">

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));
   }
 }

}</source>





Checking for Collection Containment: containsAll()

   <source lang="java">

public boolean containsAll(Collection c)</source>



true


Checking for Position from End

   <source lang="java">

public int lastIndexOf(Object element) public int lastIndexOf(Object element, int index)</source>



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


Checking for Position: where it is in the vector

   <source lang="java">

public int indexOf(Object element) public int indexOf(Object element, int index)</source>



1


Checking Vectors for Equality: Containing equivalent elements in identical order

   <source lang="java">

public boolean equals(Object o)</source>



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.



   <source lang="java">

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]);
   }
 }

}</source>



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.



   <source lang="java">

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]);
   }
 }

}</source>



A
B
C


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

   <source lang="java">

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]);
   }
 }

}</source>



A
B
C


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

   <source lang="java">

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]

  • /</source>





Copy Elements of Vector to ArrayList with Collection.copy

   <source lang="java">

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]

  • /</source>





Copying and Cloning Vectors

   <source lang="java">

public Object clone()</source>



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


Count distinct elements in a Vector

   <source lang="java">

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();
 }

}</source>





Create Java ArrayList From Enumeration which is from Vector

   <source lang="java">

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]

  • /</source>





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

   <source lang="java">

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());
 }

}</source>



[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

   <source lang="java">

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());
 }

}</source>





Enumerating through the Elements

   <source lang="java">

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());
   }
 }

}</source>



A
B


Finding all the positions for a single element

   <source lang="java">

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++;
     }
   }
 }

}</source>



1
8


Find maximum element of Java Vector

   <source lang="java">

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</source>





Find Minimum element of Java Vector

   <source lang="java">

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);
 }

}</source>





Get Enumeration over Java Vector

   <source lang="java">

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());
 }

}</source>





Getting by Position

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



   <source lang="java">

public Object firstElement() public Object lastElement()</source>



A
B


Getting elements by Index with Generics

   <source lang="java">

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);
 }

}</source>



Hello


Getting elements by Index without generics

   <source lang="java">

public Object get(int index) public Object elementAt(int index) (starting from zero)</source>



Hello


Java vectors: dynamically sized arrays with synchronized access.

Creating Vectors



   <source lang="java">

public Vector() public Vector(int initialCapacity) public Vector(int initialCapacity, int capacityIncrement)</source>



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


Multidimensional Vectors (Vectors of Vectors)

   <source lang="java">

MyType o = (MyType)((Vector)vector.elementAt(3)).elementAt(2);</source>



A


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

   <source lang="java">

public boolean remove(Object element) public boolean removeElement(Object element)</source>



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

   <source lang="java">

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

  • /</source>





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

   <source lang="java">

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);
 }

}</source>



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


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

   <source lang="java">

public void clear() public void removeAllElements()</source>



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


Removing Another Collection: public boolean removeAll(Collection c)

   <source lang="java">

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);
 }
 

}</source>



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


Removing Single Elements

   <source lang="java">

public Object remove(int index) public void removeElementAt(int index)</source>



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

   <source lang="java">

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]

  • /</source>





Replace all occurrences of specified element of Java Vector

   <source lang="java">

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);
 }

}</source>





Replace an element at specified index of Java Vector

   <source lang="java">

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);
   }
 }

}</source>





Replacing Elements

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



   <source lang="java">

public Object set(int index, Object element) public void setElementAt(Object obj, int index)</source>



[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.



   <source lang="java">

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);
 }

}</source>



[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

   <source lang="java">

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]

  • /</source>





Search an element of Java Vector

   <source lang="java">

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"));
 }

}</source>





Search an element of Vector from specific index

   <source lang="java">

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));
 }

}</source>





Serializing Vector

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



   <source lang="java">

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());
   }
 }

}</source>



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:



   <source lang="java">

public boolean isEmpty()</source>



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


Shuffle elements of Vector

   <source lang="java">

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]

  • /</source>





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.



   <source lang="java">

public int size() public void setSize(int newSize)</source>



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


Sort Vector in descending order using comparator

   <source lang="java">

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);
 }

}</source>





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:



   <source lang="java">

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());
 }

}</source>



[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

   <source lang="java">

public List subList(int fromIndex, int toIndex)</source>



B


Swap elements of Vector

   <source lang="java">

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);
 }

}</source>





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

   <source lang="java">

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);
 }

}</source>



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


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

   <source lang="java">

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);
 }

}</source>



true


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

   <source lang="java">

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));
 }
 

}</source>



[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()

   <source lang="java">

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());
 }

}</source>



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.



   <source lang="java">

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);
 }

}</source>



[]
[A, C]


Unmodifiable Vector Adapter

   <source lang="java">

/*

* $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();
 }

}</source>





Using Iterator to loop through Vector elements

   <source lang="java">

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());
   }
 }

}</source>



A
B


Using list Iterator to loop through the vector

   <source lang="java">

public ListIterator listIterator() public ListIterator listIterator(int index)</source>



A
B


Vector Immutability

   <source lang="java">

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);
 }

}</source>



[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.

   <source lang="java">

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());
   
 }

}</source>



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