search
HomeJavaJavagetting StartedWhat are the differences between ArrayList, LinkedList and Vector?
What are the differences between ArrayList, LinkedList and Vector?Aug 06, 2020 pm 04:01 PM
arraylistlinkedlistvector

What are the differences between ArrayList, LinkedList and Vector?

1. Analysis from the storage data structure

(Recommended tutorial: java introductory tutorial)

ArrayList: Array

Vector: Array

LinkedList: Doubly linked list

Array: You can quickly search based on the subscript, so in most cases, the query is fast.

But if you want to perform addition and deletion operations, you will need to move all the elements behind the modified element, so the overhead of additions and deletions is relatively large, and the execution efficiency of the array's addition and deletion operations is low. ArrayList and Vector, which use arrays as data storage structures, also have these characteristics. The query speed is fast (can be retrieved directly based on the subscript, which is faster than iterative search), and the addition and deletion are slow.

Linked list: It is convenient to add and delete elements. To add or delete an element, you only need to deal with the references between nodes. Just like people holding hands in a row, if you want to add or delete someone, you only need to change the two people nearby to hold hands with another person, and it will have no effect on the people who are already holding hands. The resources and time consumed by substitution are the same no matter where.

But the query is inconvenient. It needs to be compared one by one, and it cannot be directly searched based on the subscript. LinkedList, which is stored in a linked list structure, also has these characteristics. It is easy to add and delete, but slow to query (referring to random query, not sequential query).

2. Analysis from the perspective of inheritance

What are the differences between ArrayList, LinkedList and Vector?

They all implement the List interface, which means they all implement get(int location ), remove(int location) and other "functions to obtain and delete nodes based on index values".

(Video tutorial recommendation: java video tutorial)

It is easy to get the value of the array structure according to the subscript, and the implementation of the LinkedList bidirectional list is also relatively simple, by counting the index value To implement, search starts from 1/2 the length of the linked list. If the subscript is larger, it will start searching from the head of the list. If it is smaller, it will start searching from the end of the list.

3. Analysis from the perspective of concurrency safety

Vector: thread safety

ArrayList: non-thread safety

LinkedList: non-thread Security

4. Data growth analysis

Vector: By default, the growth is twice the length of the original array. Speaking of default, it means that he can actually set the initialization size independently.

ArrayList: Automatically grows 50% of the original array.

The above is the detailed content of What are the differences between ArrayList, LinkedList and Vector?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Apr 27, 2023 pm 03:40 PM

一、Iterator和foreach的区别多态差别(foreach底层就是Iterator)Iterator是一个接口类型,他不关心集合或者数组的类型;for和foreach都需要先知道集合的类型,甚至是集合内元素的类型;1.为啥说foreach底层就是Iterator编写的代码:反编译代码:二、foreach与iterator时remove的区别先来看阿里java开发手册但1的时候不会报错,2的时候就会报错(java.util.ConcurrentModificationException)首

如何在Java中检查ArrayList是否包含某个元素?如何在Java中检查ArrayList是否包含某个元素?Sep 03, 2023 pm 04:09 PM

您可以利用List接口的contains()方法来检查列表中是否存在对象。contains()方法booleancontains(Objecto)如果此列表包含指定的元素,则返回true。更正式地说,如果且仅当此列表包含至少一个元素e,使得(o==null?e==null:o.equals(e)),则返回true。参数c-要测试其在此列表中是否存在的元素。返回值如果此列表包含指定的元素,则返回true。抛出ClassCastException-如果指定元素的类型与此列表不兼容(可选)。NullP

使用LinkedList类的removeLast()方法删除链表中的最后一个元素使用LinkedList类的removeLast()方法删除链表中的最后一个元素Jul 24, 2023 pm 05:13 PM

使用LinkedList类的removeLast()方法删除链表中的最后一个元素LinkedList是Java集合框架中常见的一种数据结构,它以双向链表的形式存储元素。通过LinkedList类提供的方法,我们可以方便地对链表进行操作,例如添加、删除和修改元素。在某些场景下,我们可能需要删除链表中的最后一个元素。LinkedList类提供了removeLas

使用java的ArrayList.remove()函数移除ArrayList中的元素使用java的ArrayList.remove()函数移除ArrayList中的元素Jul 24, 2023 pm 01:21 PM

使用java的ArrayList.remove()函数移除ArrayList中的元素在Java中,ArrayList是一种常用的集合类,用于储存和操作一组元素。ArrayList类提供了许多方法来增删改查集合中的元素。其中一个使用频率较高的方法是remove(),它可以移除ArrayList中的元素。ArrayList的remove()方法有两种重载形式,一

使用java的ArrayList.clear()函数清空ArrayList中的元素使用java的ArrayList.clear()函数清空ArrayList中的元素Jul 24, 2023 pm 02:04 PM

使用Java的ArrayList.clear()函数清空ArrayList中的元素在Java编程中,ArrayList是一种非常常用的数据结构,它可以动态地存储和访问元素。然而,在某些情况下,我们可能需要清空ArrayList中的所有元素,以便重新使用或释放内存。这时,就可以使用ArrayList的clear()函数来实现。ArrayList.clear()

Java中ArrayList初始化容量大小为10的原因是什么Java中ArrayList初始化容量大小为10的原因是什么May 10, 2023 pm 02:19 PM

为什么HashMap的初始化容量为16?在聊ArrayList的初始化容量时,要先来回顾一下HashMap的初始化容量。这里以Java8源码为例,HashMap中的相关因素有两个:初始化容量及装载因子:/***Thedefaultinitialcapacity-MUSTbeapoweroftwo.*/staticfinalintDEFAULT_INITIAL_CAPACITY=1>1);if(newCapacity-minCapacity0)newCapacity=hugeCapacity

Java使用ArrayList类的contains()函数判断元素是否存在Java使用ArrayList类的contains()函数判断元素是否存在Jul 24, 2023 pm 07:33 PM

Java使用ArrayList类的contains()函数判断元素是否存在在Java编程中,ArrayList是一个非常常用的数据结构。它提供了一种灵活的方法来存储和操作一组数据。除了简单的添加、删除和访问元素之外,ArrayList还提供了一些有用的方法,例如contains()函数,用于判断元素是否存在于ArrayList中。contains()函数是A

在Java中从ArrayList获取唯一值在Java中从ArrayList获取唯一值Sep 04, 2023 am 08:41 AM

ArrayListisaclassofJavaCollectionFrameworkthatimplementsListInterface.Itisalinearstructurethatstoresandaccesseseachelementsequentially.Itallowsthestorageofduplicateelementshowever,thereareafewapproachesthatmayhelptogetuniquevaluesfromanArrayList.Inth

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor