Showing posts with label vector examples. Show all posts
Showing posts with label vector examples. Show all posts

Monday, 25 May 2015

How to count number of element in vector

Size() Method return number of element in the vector.

import java.util.Vector;

public class CountElement {
   
    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector is = "+vector);
      
            /*
             * Returns the number of components in this vector.
             */
        System.out.println("size = "+vector.size());
    }

}

Output:

Vector is = [cricket, hockey, football, tennish]

size = 4
 

How to shuffle elements in Vector


import java.util.Collections;
import java.util.Vector;

public class Shuffle {

    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector = "+vector);
               
        /*
         * Randomly permutes the specified list using a
         *  default source of randomness.
         */
        Collections.shuffle(vector);
      
        System.out.println("Vector after first Shuffle = "+vector);
      
        Collections.shuffle(vector);
      
        System.out.println("Vector after second Shuffle = "+vector);
      
    }
}
   
Output:
Vector = [cricket, hockey, football, tennish]

Vector after first Shuffle = [tennish, cricket, football, hockey]

Vector after second Shuffle = [cricket, hockey, tennish, football]

How to get element of vector


import java.util.Vector;

public class GetElement {
   
    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector = "+vector);
      
        /*
         * Returns the element at the specified
         * position in this Vector.
         */
        System.out.println("element at index 2 is = "+vector.get(2));
               
            }

}



Output:

Vector = [cricket, hockey, football, tennish]

element at index 2 is = football
 

How to get sub list from Vector

we can use sublist(fromindex,toindex) for get sublist from a vector.

import java.util.List;
import java.util.Vector;

public class GetSubList {
   
    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
        vector.add("java");
      
        // print vector
        System.out.println("Vector = "+vector);
               
        /*
         * Returns a view of the portion of this
         *  List between fromIndex, inclusive, and
         *  toIndex, exclusive. (If  fromIndex and toIndex are equal,
         *  the returned List is empty.) T
         */
      
     List<String> list = vector.subList(1,3);
    
     System.out.println("list is ="+list);
      
    }

}

Output:

Vector = [cricket, hockey, football, tennish, java]

list is =[hockey, football]

How to remove all element from vector

We can remove all element of vector using clear() method . clear() removes all element from the list and return empty list.


import java.util.Vector;

public class RemoveAll {
   
    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector is = "+vector);
      
        /*
         * Removes all of the elements from this
         * Vector. The Vector will be empty after
         *  this call returns (unless it throws an
         *   exception).
         */
        vector.clear();
      
        System.out.println("Vector After Clear is = "+vector);
               
    }

}


Output:

Vector is = [cricket, hockey, football, tennish]

Vector After Clear is = []

How to swap elements of vector

import java.util.Collections;
import java.util.Vector;

public class SwapElement {

    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Before swap Vector = "+vector);
      
        /*
         * Swaps the elements at the specified positions
         * in the specified list. (If the specified
         * positions are equal, invoking this method
         * leaves the list unchanged.)
         */
      
        Collections.swap(vector, 1, 3);
      
        System.out.println("After swap vector = "+vector);
               
            }   
   
}

Output:

Before swap Vector = [cricket, hockey, football, tennish]

After swap vector = [cricket, tennish, football, hockey]

Thursday, 21 May 2015

How To Shallow Copy Or Clone a Vector

import java.util.Vector;

public class CloneOfVector {
   
    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector = "+vector);
   
        /*
         * Returns a clone of this vector. The copy will
         * contain a reference to a clone of the internal
         *  data array, not a reference to the original internal
         *  data array of this Vector object.
         */
      
        Vector<String> myvector = (Vector<String>) vector.clone();
      
        for (String str : myvector) {

        System.out.println("element is = "+str);
   
         }
      
    }

}

How To Copy Vector To Array

import java.util.Vector;

public class VectorToArray {
  
    public static void main(String[] args) {
       
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
       
        // print vector
        System.out.println("Vector = "+vector);
       
        String [] arr = new String[vector.size()];
       
        //Returns an array containing all of the
        //elements in this Vector in the correct order.
       
        vector.toArray(arr);

        System.out.println("Array is :");

        for (int i = 0; i < arr.length; i++) {
    System.out.println("Element is = "+arr[i]);
      
    }
              
   }

}




Output:

Vector = [cricket, hockey, football, tennish]

Array is :

Element is = cricket

Element is = hockey

Element is = football

Element is = tennish



How to create Vector In java

import java.util.Vector;

public class CreateVector {
   
     public static void main(String[] args) {
       
         Vector<String> vector = new Vector<String>();
       
         //add element in vector
         vector.add("cricket");
         vector.add("hockey");
         vector.add("football");
         vector.add("tennish");
       
         // print vector
         System.out.println("Vector = "+vector);
               
            }
}


Output:

Vector = [cricket, hockey, football, tennish]