slug
type
status
category
date
summary
tags
password
icon
 
In Java, List and ArrayList are closely related but serve different roles. Here's a detailed comparison with examples:

1. List Interface

  • List is an interface in Java, part of the java.util package.
  • It represents a collection of elements that are ordered and allow duplicates.
  • Since it's an interface, it cannot be instantiated directly. Instead, it is implemented by classes like ArrayList, LinkedList, etc.

2. ArrayList Class

  • ArrayList is a class that implements the List interface.
  • It is a resizable array that can dynamically grow or shrink.
  • Elements in an ArrayList are stored in a contiguous memory location, making it efficient for random access.

Key Differences

Feature
List (Interface)
ArrayList (Class)
Type
Interface
Class (implements List)
Instantiation
Cannot instantiate directly
Can instantiate directly
Flexibility
Allows using different implementations
Specifically for resizable array functionality
Performance
Depends on implementation
Fast random access, slower insertion/deletion

Examples

Using ArrayList Directly

import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); // Instantiate ArrayList names.add("Alice"); names.add("Bob"); names.add("Charlie"); System.out.println(names); // Output: [Alice, Bob, Charlie] } }

Using List for Flexibility

import java.util.List; import java.util.ArrayList; public class Main { public static void main(String[] args) { List<String> names = new ArrayList<>(); // Instantiate using List reference names.add("Alice"); names.add("Bob"); names.add("Charlie"); System.out.println(names); // Output: [Alice, Bob, Charlie] } }
Why use List here?
  • You can easily switch to another implementation, like LinkedList, without changing much code:
import java.util.List; import java.util.LinkedList; public class Main { public static void main(String[] args) { List<String> names = new LinkedList<>(); // Switched to LinkedList names.add("Alice"); names.add("Bob"); names.add("Charlie"); System.out.println(names); // Output: [Alice, Bob, Charlie] } }

Choosing Between List and ArrayList

  1. Use List when:
      • You want to write generic code and allow switching implementations (e.g., LinkedList, ArrayList, Vector).
      • You prefer flexibility and abstraction over implementation specifics.
  1. Use ArrayList when:
      • You specifically need a resizable array with random access.
      • You are certain that ArrayList meets your performance requirements for your use case.

Summary

  • Use List for abstraction and flexibility.
  • Use ArrayList for a specific implementation with dynamic resizing and fast random access.
Sixth Term Examination Paper (STEP)Sample Variance and Population Variance
Loading...
现代数学启蒙
现代数学启蒙
推广现代数学🍚
最新发布
AP CSA 期末考试
2025-5-28
An introduction to AP CSA (2026)
2025-5-27
Learn Java with Fun
2025-5-27
Cracking CSA:Inheritance, Superclasses, and Subclasses(旧大纲)
2025-5-27
Cracking CSA:Inheritance and Polymorphism(旧大纲)
2025-5-27
AP CSA: inheritance in Java(旧大纲)
2025-5-27
公告
🎉现代数学启蒙(MME:Modern Mathematics Enlightenment)欢迎您🎉
-- 感谢您的支持 ---