Home > Java > javaTutorial > body text

Java Vector class detailed explanation and example code

高洛峰
Release: 2017-02-03 17:19:08
Original
1455 people have browsed it

Java Vector class

Vector’s unique functions

Vector appears earlier, earlier than collections

1: Add functions

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

2: Get the function

public Object elementAt(int index);//用get()替代
public Enumeration elements();//返回的是实现类的对象,用Iterator iterator()
Copy after login
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);
    }
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template