Home  >  Article  >  Java  >  Java Vector class detailed explanation and example code

Java Vector class detailed explanation and example code

高洛峰
高洛峰Original
2017-02-03 17:19:081459browse

Java Vector class

Vector’s unique functions

Vector appears earlier, earlier than collections

1: Add functions

public void addElement(Object obj);//用add()替代

2: Get the function

public Object elementAt(int index);//用get()替代
public Enumeration elements();//返回的是实现类的对象,用Iterator iterator()
import java.util.Enumeration;
import java.util.Vector;
  
/*
 * Vector的特有功能
 *
 * Vector出现较早,比集合更早出现
 *
 * 1:添加功能
 * public void addElement(Object obj);//用add()替代
 *
 * 2:获取功能
 * public Object elementAt(int index);//用get()替代
 * public Enumeration elements();//返回的是实现类的对象,用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();// 返回的是实现类的对象
  
      System.out.println(s);
    }
  }
}

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more articles related to Java Vector class details and example codes, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Basic data types in JAVANext article:Basic data types in JAVA