Convert an Array to a Vector : Arrays « Collections Data Structure « Java






Convert an Array to a Vector

 

/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the Java 
 * language and environment is gratefully acknowledged.
 * 
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

/** Convert an Array to a Vector. */
public class ArrayToVector {
  public static void main(String[] args) {
    Object[] a1d = { "Hello World", new Date(), Calendar.getInstance(), };
    // Arrays.asList(Object[]) --> List
    List l = Arrays.asList(a1d);

    // Vector contstructor takes Collection
    // List is a subclass of Collection
    Vector v;
    v = new Vector(l);

    // Or, more simply:
    v = new Vector(Arrays.asList(a1d));

    // Just to prove that it worked.
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
      System.out.println(e.nextElement());
    }
  }
}

           
         
  








Related examples in the same category

1.illustrates how to use some of the methods of the Arrays class:
2.Sorting, Searching, and Inserting into a sorted arraySorting, Searching, and Inserting into a sorted array
3.Demonstrate use of Arrays.sort on Booleans
4.Convert Array to Collection
5.Convert array of object to array of primitive?
6.Compare if two array is equal
7.Array Search Test
8.Array Fill Test Array Fill Test
9.Copy some items of an array into another array
10.Static methods from ArraysStatic methods from Arrays
11.Sort an arraySort an array
12.Sort an array: case-sensitive
13.Sort an array: case-insensitive
14.Java Sort byte Array
15.Java Sort char Array
16.Java Sort double Array
17.Java Sort float Array
18.Expanding an Array
19.Compare two byte type arrays
20.Compare two char type arrays
21.Compare two short type arrays
22.Compare two int type arrays
23.Compare two long type arrays
24.Compare two float type arrays
25.Compare two double type arrays
26.Filling Elements in an Array: boolean type
27.Filling Elements in an Array: byte type
28.Filling Elements in an Array: char type
29.Filling Elements in an Array: short type
30.Filling Elements in an Array: int type
31.Filling Elements in an Array: long type
32.Filling Elements in an Array: float type
33.Filling Elements in an Array: double type
34.filling object arrays:
35.fill to a contiguous range of elements in an array: boolean array
36.Fill to a contiguous range of elements in an array: byte array
37.Fill to a contiguous range of elements in an array: char array
38.Fill to a contiguous range of elements in an array: short array
39.Fill to a contiguous range of elements in an array: int array
40.Fill to a contiguous range of elements in an array: long array
41.Fill to a contiguous range of elements in an array: float array
42.Use Arrays.asList() to convert Array to List
43.Merge (or add) two arrays into one
44.Fill to a contiguous range of elements in an array: double array
45.Fill to a contiguous range of elements in an array: String array
46.Minimum and maximum number in array
47.Finding an Element in a Sorted Array
48.Shuffle elements of an array
49.Shifting Elements in an Array: Shift all elements right by one
50.Shifting Elements in an Array: Shift all elements left by one
51.Use java.util.Arrays.deepToString() to dump the multi-dimensional arrays
52.use Arrays.copyOf to copy array
53.If the element is a primitive type.
54.When comparing Object arrays, null elements are equal. If the elements are not null, Object.equals() is used.
55.Comparing Arrays: null arrays are equal
56.Compare two boolean arrays and null