ArrayList 和 Vectors 都实现了 List 接口,并且都使用**(动态调整大小)数组**作为其内部数据结构,就像使用普通数组一样。
句法:
ArrayList<T> al = new ArrayList<T>();
Vector<T> v = new Vector<T>();
ArrayList 和 Vector 的主要区别:
// Java Program to illustrate use of ArrayList
// and Vector in Java
import java.io.*;
import java.util.*;
class Solution
{
public static void main (String[] args)
{
// creating an ArrayList
ArrayList<String> al = new ArrayList<String>();
// adding object to arraylist
al.add("Practice.dailycodebuffer.org");
al.add("quiz.dailycodebuffer.org");
al.add("code.dailycodebuffer.org");
al.add("contribute.dailycodebuffer.org");
// traversing elements using Iterator'
System.out.println("ArrayList elements are:");
Iterator it = al.iterator();
while (it.hasNext())
System.out.println(it.next());
// creating Vector
Vector<String> v = new Vector<String>();
v.addElement("Practice");
v.addElement("quiz");
v.addElement("code");
// traversing elements using Enumeration
System.out.println("\nVector elements are:");
Enumeration e = v.elements();
while (e.hasMoreElements())
System.out.println(e.nextElement());
}
}
输出:
ArrayList elements are:
Practice.dailycodebuffer.org
quiz.dailycodebuffer.org
code.dailycodebuffer.org
contribute.dailycodebuffer.org
Vector elements are:
Practice
quiz
code
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.dailycodebuffer.com/vector-vs-arraylist-in-java/
内容来源于网络,如有侵权,请联系作者删除!