Java中Vector和ArrayList之间的区别

向量与Java中的ArrayList (Vector vs ArrayList in Java)

1)Java中的向量 (1) Vector in Java)

  • Vector is an implementation class of List interface.

    Vector是List接口的实现类。

  • Vector is a legacy class that means it does not fully support the collection framework.

    Vector是一个遗留类,这意味着它不完全支持收集框架。

  • Vector class introduced in earlier versions of java so these classes need to be re-engineered to support collection framework.

    在Java的早期版本中引入的Vector类,因此需要对这些类进行重新设计以支持集合框架。

  • When we create a Vector object then default capacity to store the element is 10 so if it reaches to its maximum capacity then a new vector object will be created and capacity will be doubled of its current capacity of the newly created object in case if we store 11th element in the Vector list.

    当我们创建一个Vector对象时,默认存储容量为10,因此,如果达到最大容量,则将创建一个新的vector对象,并且如果我们存储该对象,其容量将是新创建对象的当前容量的两倍。向量列表中的第11个元素。

  • Vector is thread-safe that means only one thread is allowed to operate on one object that's why Vector is thread-safe.

    Vector是线程安全的,这意味着只允许一个线程对一个对象进行操作,这就是Vector是线程安全的原因。

  • We will see given how to create a Vector object with default constructor.

    我们将看到给出的如何使用默认构造函数创建Vector对象的方法。

    Syntax:

    句法:

    Vector v = new Vector();
    
  • The default size of the Vector object is 10 so if reached to its maximum capacity then-new capacity will be given below:

    Vector对象的默认大小为10,因此,如果达到其最大容量,则会在下面给出新容量:

    New capacity = 2*Current_Capacity;

    新容量= 2 * Current_Capacity;

  • Vector class implements RandomAccess interface so that we can access random element with the same speed.

    Vector类实现RandomAccess接口,以便我们可以以相同的速度访问随机元素。

Example:

例:

import java.util.*;

class VectorClass {
    public static void main(String[] args) {
        // Creating a Vector instance
        Vector v = new Vector();

        // Display default capacity of Vector object
        System.out.println("Current Capacity:" + v.capacity());

        // Loop to print 10 elements
        for (int i = 0; i < 10; ++i)
            v.addElement(i);

        // Display capacity of adding till 10 elements
        System.out.println("Capacity of adding till 10 elements is:" + v.capacity());

        v.addElement(11);

        // New capacity if it reached its maximum capacity
        System.out.println("New Capacity after adding 11th element will be " + v.capacity());
    }
}

Output

输出量

E:\Programs>javac VectorClass.java
Note: VectorClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Programs>java VectorClass
Current Capacity:10
Capacity of adding till 10 elements is:10
New Capacity after adding 11th element will be 20

2)Java中的ArrayList (2)ArrayList in Java)

  • ArrayList is an implementation class of List interface.

    ArrayList是List接口的实现类。

  • ArrayList is not a legacy class that means it supports the collection framework and we don't need to re-engineer this class.

    ArrayList不是遗留类,这意味着它支持收集框架,我们不需要重新设计此类。

  • When we create an ArrayList object then default capacity to store the element is 10 so if it reaches to its maximum capacity than a new ArrayList object will be created and capacity will increase like (half of its current capacity+1) of newly created object in case if we store 11th element in the Vector list the new capacity will be 16.

    当我们创建一个ArrayList对象时,默认存储容量为10,因此,如果该元素达到其最大容量,则将创建一个新的ArrayList对象,并且容量将增加,如新创建的对象的(其当前容量的一半)。如果我们在Vector列表中存储第11个元素,则新容量为16。

  • ArrayList is not thread-safe because it is not synchronized.

    ArrayList不是线程安全的,因为它不同步。

  • We will see, How to create an object of ArrayList by using ArrayList class.

    我们将看到,如何使用ArrayList类创建ArrayList对象。

    Syntax:

    句法:

    ArrayList object_name = new ArrayList();
    
  • The default size of the ArrayList is 10.

    ArrayList的默认大小为10。

  • Once the ArrayList reaches its maximum capacity then a new object will be created with new capacity and the new capacity will be calculated like this:

    一旦ArrayList达到最大容量,将创建一个具有新容量的新对象,并按以下方式计算新容量:

    New_capacity = Current_Capacity*3/2 + 1

    新容量=当前容量* 3/2 + 1

  • The ArrayList class implements RandomAccess interface so that we can access random element with the same speed.

    ArrayList类实现RandomAccess接口,以便我们可以以相同的速度访问随机元素。

Example:

例:

import java.util.*;

class ArrayListClass {
    public static void main(String[] args) {
        // Creating a ArrayList instance
        ArrayList al = new ArrayList();

        // Loop to print 10 elements
        for (int i = 0; i < 10; ++i)
            al.add(i);

        // Display capacity of adding till 10 elements
        System.out.println("Capacity of adding till 10 elements is:" + al.size());

        // Loop to print again 6 elements
        for (int j = 0; j < 6; ++j)
            al.add(j);

        // New capacity if it reached its maximum capacity
        System.out.println("New Capacity after adding 6 more element will be " + al.size());
    }
}

Output

输出量

E:\Programs>javac ArrayListClass.java
Note: ArrayListClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Programs>java ArrayListClass
Capacity of adding till 10 elements is:10
New Capacity after adding 6 more element will be 16


翻译自: https://www.includehelp.com/java/differences-between-vector-and-arraylist-in-java.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值