Java Vector class details and example code

  • 2020-06-01 09:56:01
  • OfStack

Java Vector class

Unique features of Vector

Vector appears earlier, earlier than collections

1: add functionality


 public void addElement(Object obj);// with add() alternative 

2: get function


public Object elementAt(int index);// with get() alternative 
public Enumeration elements();// Returns the object of the implementation class Iterator iterator()


import java.util.Enumeration;
import java.util.Vector;
 
/*
 * Vector Specific function 
 *
 * Vector It appears earlier, earlier than the set 
 *
 * 1 : add function 
 * public void addElement(Object obj);// with add() alternative 
 *
 * 2 : get function 
 * public Object elementAt(int index);// with get() alternative 
 * public Enumeration elements();// Returns the object of the implementation class Iterator iterator()
 * */
 
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
 
    Vector v = new Vector();
 
    v.addElement("hello");
    v.addElement("world");
    v.addElement("java");
 
    for (int i = 0; i < v.size(); i++) {
      String s = (String) v.elementAt(i);
 
      System.out.println(s);
    }
 
    System.out.println("---------------");
 
    for (Enumeration en = v.elements(); en.hasMoreElements();) {
      String s = (String) en.nextElement();//  Returns an object of the implementation class 
 
      System.out.println(s);
    }
  }
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: