Use Vector in java.util : Vector « Collections Data Structure « Java






Use Vector in java.util

Use Vector in java.util
  
import java.util.Vector;

public class VectorBenchmark1 {

  public static final int MaxSize = 100000;

  public static final int NTRIES = 10;

  public static void main(String[] args) {
    Vector v = new Vector();

    long start = System.currentTimeMillis();
    for (int i = 0; i < MaxSize; i++)
      v.add(new Integer(i));
    long end = System.currentTimeMillis();
    System.out.println("Allocating vector elements: " + (end - start)
        + " milliseconds");

    Integer[] integerArray = new Integer[1];
    start = System.currentTimeMillis();
    for (int i = 0; i < MaxSize; i++) {
      if (i >= integerArray.length) {
        Integer[] b = new Integer[i * 2];
        System.arraycopy(integerArray, 0, b, 0, integerArray.length);
        integerArray = b;
      }
      integerArray[i] = new Integer(i);
    }
    end = System.currentTimeMillis();
    System.out.println("Allocating array elements:  " + (end - start)
        + " milliseconds");

    start = System.currentTimeMillis();
    for (int j = 0; j < NTRIES; j++)
      for (int i = 0; i < MaxSize; i++) {
        Integer r = (Integer) v.get(i);
        v.set(i, new Integer(r.intValue() + 1));
      }
    end = System.currentTimeMillis();
    System.out.println("Accessing vector elements:  " + (end - start)
        + " milliseconds");

    start = System.currentTimeMillis();
    for (int j = 0; j < NTRIES; j++)
      for (int i = 0; i < MaxSize; i++) {
        Integer r = integerArray[i];
        integerArray[i] = new Integer(r.intValue() + 1);
      }
    end = System.currentTimeMillis();
    System.out.println("Accessing array elements:   " + (end - start)
        + " milliseconds");
  }
}
           
         
    
  








Related examples in the same category

1.Finding elements in a vectorFinding elements in a vector
2.Serializing a vectorSerializing a vector
3.Save Vector to file
4.Create Vector test
5.Find Vector Find Vector
6.Remove Element in Vector
7.Sequence Sequence
8.Vector Util
9.Iterator out of Vector
10.Copy Elements of One Java Vector to Another Java Vector
11.Copy Elements of Vector to Java ArrayList
12.Create Java ArrayList From Enumeration
13.Find maximum element of Java Vector
14.Find Minimum element of Java Vector
15.Get Enumeration over Java Vector
16.Perform Binary Search on Java Vector
17.Replace All Elements Of Java Vector
18.Replace all occurrences of specified element of Java Vector
19.Reverse order of all elements of Java Vector
20.Shuffle elements of Java Vector
21.Swap elements of Java Vector
22.Sort Java Vector in descending order using comparator
23.Enumerate through a Vector using Java Enumeration
24.Append all elements of other Collection to Java Vector
25.Copy all elements of Java Vector to an Object Array
26.Get Size of Java Vector and loop through the elements
27.Get Sub List of Java Vector Example
28.Insert all elements of other Collection to Specified Index of Java Vector
29.Iterate through elements Java Vector using Iterator
30.Iterate through elements Java Vector using ListIterator
31.Remove all elements from Java Vector
32.Remove an element from specified index of Java Vector
33.Remove specified element from Java Vector
34.Replace an element at specified index of Java Vector
35.Search an element of Java Vector
36.Search an element of Java Vector from specific index
37.Set size of Java Vector Example
38.Sort elements of Java Vector
39.Generic Vector with String
40.Count distinct elements in a Vector
41.Unmodifiable Vector Adapter
42.Adaptive extension of the java.util.Vector class