Affichage des articles dont le libellé est java collection framework vector. Afficher tous les articles
Affichage des articles dont le libellé est java collection framework vector. Afficher tous les articles

JAVA - How To Use Vector Collection In Java

How To Use Vector Collection In Java - Java Vector Example

                                                                                                                                                            
java vector collection

In this java Collection tutorial we will see How To Use A Vector In Java NetBeans
With Some Java Vector Example of Methodes.
1: how to create a Vector
2: how to dispaly data from it
3: how to add an elements in the first
4: how to add an elements in the end
5: how to clear all elements from it
and more........

Source Code:


package javaapp;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class Woirk {

    public static void main(String[] args){
       
       //create vector one v
       Vector v = new Vector(); 
       v.add(54);
       v.add("java");
       v.add("csharp");
       v.add(100);
       v.add("java");
       v.add("pc");
       v.add("laptop");
       v.add(10);
       System.out.println(v);
       
       //create vector two v2
       Vector v2 = new Vector();
       v2.add("java");
       v2.add(2015);
       v2.add("V2");
       
       //add v2 to v
       v.addAll(v2);
       System.out.println(v);
      
       //add element at the specified position
       v.add(2, 222);
       System.out.println(v);
       
       //create a vector three v3
       Vector v3 = new Vector();
       v3.add("V3");
       v3.add("VCT");
       
       v.addAll(v3);
       
     //add collection at the specified position to another collection
       v.addAll(0, v3);
       System.out.println(v);
       
       //add element and incress the size by one
       v.addElement("AddElement");
       
       //insert element at the specified position
       v.insertElementAt("insertElementAt", 5);
       
       //get the capacity of the vector
       int vectorCapacity = v.capacity();
       System.out.println(vectorCapacity);
       
       //check if an element exist in the collection
       boolean exist = v.contains(2015);
       System.out.println("2015 Exist: "+exist);
       
       //check if a collection exist in the vector
       boolean containCollection = v.containsAll(v3);
       System.out.println("V3 Exist: "+containCollection);
      
       //get the object by his index
       Object obj = v.elementAt(1);
       System.out.println(obj);
    
       //return Enumeration of the victor
       Enumeration<Object> e = v.elements();
       while(e.hasMoreElements()){
       System.out.print(e.nextElement()+" - ");
       }
       System.out.println();
       
       //insert element in the specified place
       v.set(0,1111);
       v.set(1, 2222);
       System.out.println(v);
       
       // get a sub list between two index
       List subV = v.subList(2,7);
       System.out.println(subV);
       
       //how to use toArray() methode with vector
       Object[] obj2 = v.toArray();
       for(Object ob : obj2){
           System.out.print(ob+", ");
       }
       System.out.println();
       
      //how to use toArray(T[] a) methode with vector
       Object[] ob = v.toArray(new Object[0]);
       for(Object o : ob){
           System.out.print(o+", ");
       }
       System.out.println();
       
       //display all vector items with iterator
       Iterator it = v.iterator();
       while(it.hasNext()){
       System.out.print(it.next()+", ");
       }
       System.out.println();
       
       //create a copy from a vector
       Object[] copy = new Object[v.size()];
       v.copyInto(copy);
       for(Object o : copy){
           System.out.print(o+", ");
       }System.out.println();
       
       //clone the vector
       Object VectorClone = v.clone();
       System.out.println(VectorClone);
       
       //get the element at a specified index
       Object elementAt = v.elementAt(3);
       System.out.println(elementAt);
       
       //check the collection is equals to another collection
       System.out.println(v.equals(v));
       
       //return the element at a specified index
       System.out.println(v.get(0));
       
 //Returns the index of the first occurrence of the element in the vector
       // -1 if not exist
       System.out.println(v.indexOf("java"));
       
       /*
  get the index of the first occurrence of an element after the given index
       */
       System.out.println(v.indexOf("java",4));
       
       //return the last index of an element
       System.out.println(v.lastIndexOf("java"));
       
     /*
  get the index of the last occurrence of an element after the given index
       */
       System.out.println(v.lastIndexOf("java",4));
       
       //return the last element of the victor
       System.out.println(v.lastElement());
       
       //check if the vector is empty
       System.out.println(v.isEmpty());
       
       //remove element by index
       Object removedByIndex = v.remove(0);
        System.out.println(removedByIndex);
     
        //remove element by his value
        //only the first occurrence
        boolean removedByVal = v.remove("VCT");
        System.out.println(removedByVal);
        
        //remove a collection from the victor
        boolean removeCollection = v.removeAll(v3);
        System.out.println(removeCollection);
        
        //the element 2015 will be deleted
        v.removeElement(2015);
        
        //the element who has the index 2 will be deleted
        v.removeElementAt(2);
        
        //return the victor size
        int vSize = v.size();
        System.out.println(vSize);
        
        //clear all elements
        v.clear();
        //clear all elements and set the size to zero
        v.removeAllElements();
        vSize = v.size();
        System.out.println(vSize);
        v.removeAllElements();
    }
}

//OUTPUT:

[54, java, csharp, 100, java, pc, laptop, 10]
[54, java, csharp, 100, java, pc, laptop, 10, java, 2015, V2]
[54, java, 222, csharp, 100, java, pc, laptop, 10, java, 2015, V2]
[V3, VCT, 54, java, 222, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT]
20
2015 Exist: true
V3 Exist: true
VCT
V3 - VCT - 54 - java - 222 - insertElementAt - csharp - 100 - java - pc - laptop - 10 - java - 2015 - V2 - V3 - VCT - AddElement - 
[1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement]
[54, java, 222, insertElementAt, csharp]
1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement, 
1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement, 
1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement, 
1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement, 
[1111, 2222, 54, java, 222, insertElementAt, csharp, 100, java, pc, laptop, 10, java, 2015, V2, V3, VCT, AddElement]
java
true
1111
3
8
12
3
AddElement
false
1111
true
true
13
0