How do I convert a Vector into an array?

This example demonstrates how to convert a Vector object into an array. You can use the Vector.copyInto(Object[] array) method to create a copy of the Vector object in array form.

package org.kodejava.example.util;

import java.util.Vector;

public class VectorToArray {
    public static void main(String[] args) {
        Vector<Integer> vector = new Vector<>();
        vector.add(10);
        vector.add(20);
        vector.add(30);
        vector.add(40);
        vector.add(50);

        // Declares and initializes an Integer array.
        Integer[] numbers = new Integer[vector.size()];

        // Copies the components of this vector into the specified
        // array of Integer
        vector.copyInto(numbers);

        for (Integer number : numbers) {
            System.out.println("Number: " + number);
        }
    }
}        

The result of the code snippet above:

Number: 10
Number: 20
Number: 30
Number: 40
Number: 50

How do I read Vector object using Enumeration?

The following code snippets show you how to iterate and read java.util.Vector elements using the java.util.Enumeration.

package org.kodejava.example.util;

import java.util.Enumeration;
import java.util.Vector;

public class VectorEnumeration {

    public static void main(String[] args) {
        Vector<String> data = new Vector<>();
        data.add("one");
        data.add("two");
        data.add("three");

        StringBuilder sb = new StringBuilder("Data: ");

        // Iterates vector object to read it elements
        for (Enumeration enumeration = data.elements();
             enumeration.hasMoreElements();) {
            sb.append(enumeration.nextElement()).append(",");
        }

        // Delete the last ","
        sb.deleteCharAt(sb.length() - 1);
        System.out.println(sb.toString());
    }
}

How do I use the Stack class in Java?

Stack is an extension of the java.util.Vector class that provided a LIFO (last-in-first-out) data structure. This class provide the usual method such as push() and pop(). The peek method is used the get the top element of the stack without removing the item.

package org.kodejava.example.util;

import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<Integer>();

        // We stored some values in the stack object.
        for (int i = 0; i < 10; i++) {
            stack.push(i);
            System.out.print(i + " ");
        }
        System.out.println("");

        // Searching for an item in the stack. The position returned
        // as the distance from the top of the stack. Here we search
        // for the 3 number in the stack which is in the 7th row of
        // the stack.
        int position = stack.search(3);
        System.out.println("Search result position: " + position);

        // The current top value of the stack
        System.out.println("Stack top: " + stack.peek());

        // Here we popping out all the stack object items.
        while (!stack.empty()) {
            System.out.print(stack.pop() + " ");
        }
    }
}

The result of the code snippet:

0 1 2 3 4 5 6 7 8 9 
Search result position: 7
Stack top: 9
9 8 7 6 5 4 3 2 1 0