Home > Java > javaTutorial > body text

The difference between ArrayList and LinkedList in Java

王林
Release: 2023-09-06 19:05:07
forward
1557 people have browsed it

The difference between ArrayList and LinkedList in Java

ArrayList and LinkedList are both implementations of the List interface in Java. Both classes are asynchronous. But there are certain differences.

The following are the important differences between ArrayList and LinkedList methods.

gentlemen. No.

Key

ArrayList

LinkedList

1

Internal implementation

ArrayList internally uses a dynamic array to store its elements .

LinkedList uses bidirectional linking to store a list of its elements.

2

Operation

ArrayList is slow because of the array Operation is slower.

Node-based LinkedList is faster because it doesn’t require as much bit shifting.

3

Implements

ArrayList only implements List.

LinkedList implements List and Queue. It can also act as a queue.

4

Access

ArrayList stores and accesses data faster.

LinkedList processes data faster.

ArrayList example with LinkedList

JavaTester.java

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class JavaTester {
   public static void main(String args[]) {
      List<String> list = new ArrayList<>();
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      List<String> list1 = new LinkedList<>();
      list1.add("A");
      list1.add("B");
      list1.add("C");
      list1.add("D");
      System.out.println(list);
      System.out.println(list1);
   }
}
Copy after login

Output

[A, B, C, D]
[A, B, C, D]
Copy after login

The above is the detailed content of The difference between ArrayList and LinkedList in Java. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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