Home  >  Article  >  Java  >  The difference between ArrayList and LinkedList in Java

The difference between ArrayList and LinkedList in Java

王林
王林forward
2023-09-06 19:05:071552browse

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);
   }
}

Output

[A, B, C, D]
[A, B, C, D]

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete