Vector


Collection in java with examples




Vector

The java.util.Vector class implements a growable array of object.
A Vector is basically similar to ArrayList,but Vector methods are synchronized for thread safety.
Since ArrayLists are unsynchronized and therefore faster than Vecors but less secure in a multithreaded environment.
Vector is the only class other than ArrayList to implement RandomAccess.

Creation of Vector
  • Vector v1 = new Vector(); // allows old or new methods
  • List v2 = new Vector(); // allows only new(List) methods.
  • v1.add(new Integer("200")); // For adding element

import java.util.Vector;
 public class RajniVector {
    public static void main (String[] args) {
        Vector<String>  vRajni = new  Vector<String>();
       
        // add vector elements
        vRajni.add ("Vector Object 1");
        vRajni.add ("Vector Object 2");
        vRajni.add ("Vector Object 3");
        vRajni.add ("Vector Object 4");
        vRajni.add ("Vector Object 5");

        // add vector element at index
        vRajni.add (3, "Element at given position");

        // vc.size() inform number of elements in Vector
        System.out.println ("Vector Size :" + vRajni.size());

        // get elements of Vector
        for(int i=0; i<vRajni.size(); i++)
        {
          System.out.println ("Vector Element " + i + " : " + vRajni.get (i));
        }
    }
 }

Output :
Vector Size :6
Vector Element 0 :Vector Object 1
Vector Element 1 :Vector Object 2
Vector Element 2 :Vector Object 3
Vector Element 3 :Element at given position
Vector Element 4 :Vector Object 4
Vector Element 5 :Vector Object 5

           More methods related to Vector.

You must want to read also about ArrayList , Set Interface,Map Interface

2 comments:

  1. can you put examples for tree list

    ReplyDelete
  2. public class Tree {

    private Node rootElement;

    public Tree() {
    super();
    }

    public Node getRootElement() {
    return this.rootElement;
    }

    public void setRootElement(Node rootElement) {
    this.rootElement = rootElement;
    }

    public List> toList() {
    List> list = new ArrayList>();
    walk(rootElement, list);
    return list;
    }


    public String toString() {
    return toList().toString();
    }

    private void walk(Node element, List> list) {
    list.add(element);
    for (Node data : element.getChildren()) {
    walk(data, list);
    }
    }
    }

    public class Node {

    public T data;
    public List> children;

    public Node() {
    super();
    }

    public Node(T data) {
    this();
    setData(data);
    }

    public List> getChildren() {
    if (this.children == null) {
    return new ArrayList>();
    }
    return this.children;
    }

    public void setChildren(List> children) {
    this.children = children;
    }

    public int getNumberOfChildren() {
    if (children == null) {
    return 0;
    }
    return children.size();
    }
    public void addChild(Node child) {
    if (children == null) {
    children = new ArrayList>();
    }
    children.add(child);
    }

    public void insertChildAt(int index, Node child) throws IndexOutOfBoundsException {
    if (index == getNumberOfChildren()) {
    // this is really an append
    addChild(child);
    return;
    } else {
    children.get(index); //just to throw the exception, and stop here
    children.add(index, child);
    }
    }

    public void removeChildAt(int index) throws IndexOutOfBoundsException {
    children.remove(index);
    }

    public T getData() {
    return this.data;
    }

    public void setData(T data) {
    this.data = data;
    }

    public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("{").append(getData().toString()).append(",[");
    int i = 0;
    for (Node e : getChildren()) {
    if (i > 0) {
    sb.append(",");
    }
    sb.append(e.getData().toString());
    i++;
    }
    sb.append("]").append("}");
    return sb.toString();
    }
    }

    ReplyDelete