杰瑞科技汇

Java new ArrayList时,容量和初始值如何设置?

Of course! Creating a new ArrayList in Java is a fundamental task. Here’s a comprehensive guide covering the most common ways, their differences, and best practices.

Java new ArrayList时,容量和初始值如何设置?-图1
(图片来源网络,侵删)

The Short and Sweet Answer

The most common and recommended way to create an ArrayList is:

import java.util.ArrayList;
// Creates an empty ArrayList that can hold Strings
ArrayList<String> myList = new ArrayList<>(); 

The Most Common Way (Using the Diamond Operator <>)

This is the modern, type-safe way to create an ArrayList. You specify the type of elements the list will hold (e.g., String, Integer, MyObject) in angle brackets.

Syntax: ArrayList<Type> listName = new ArrayList<>();

Explanation:

Java new ArrayList时,容量和初始值如何设置?-图2
(图片来源网络,侵删)
  • ArrayList<Type>: Declares a variable listName that will reference an ArrayList capable of holding elements of Type.
  • new ArrayList<>(): Creates a new, empty ArrayList instance. The empty <> is called the "diamond operator". It tells the compiler to infer the type from the left-hand side of the assignment. This was introduced in Java 7 to reduce code repetition.

Example:

import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        // Create an ArrayList of Strings
        ArrayList<String> names = new ArrayList<>();
        // Add elements to the list
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        // Print the list
        System.out.println(names); // Output: [Alice, Bob, Charlie]
    }
}

Creating an ArrayList with an Initial Capacity

An ArrayList is internally backed by an array. When you add elements and the internal array becomes full, Java needs to create a larger array and copy all the elements over. This can be a performance hit.

If you have a good estimate of how many elements your list will hold, you can specify an initial capacity to avoid this resizing process.

Syntax: ArrayList<Type> listName = new ArrayList<>(initialCapacity);

Java new ArrayList时,容量和初始值如何设置?-图3
(图片来源网络,侵删)

Example:

import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        // Create an ArrayList with an initial capacity of 10
        ArrayList<Integer> numbers = new ArrayList<>(10);
        System.out.println("Initial size: " + numbers.size()); // Output: 0
        System.out.println("Initial capacity: " + numbers.size()); // Note: capacity isn't directly exposed, but this is the idea.
        // Add more than 10 elements to see the internal resizing (which is now minimized)
        for (int i = 0; i < 15; i++) {
            numbers.add(i);
        }
        System.out.println("Size after adding 15 elements: " + numbers.size());
        // Output: Size after adding 15 elements: 15
    }
}

Note: The size() method returns the number of elements, not the internal capacity. Capacity is an implementation detail, but setting it correctly can improve performance.


Creating an ArrayList from an Existing Collection

You can create a new ArrayList that is a copy of another Collection (like another ArrayList, LinkedList, HashSet, etc.).

Syntax: ArrayList<Type> listName = new ArrayList<>(otherCollection);

Example:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        // 1. Create a List from an array
        List<String> originalList = Arrays.asList("Apple", "Banana", "Cherry");
        // 2. Create a new ArrayList from the original list
        // This creates a *shallow copy*. The new list has new references to the same objects.
        ArrayList<String> copiedList = new ArrayList<>(originalList);
        System.out.println("Original List: " + originalList);
        System.out.println("Copied List:   " + copiedList);
        // Modifying the copied list does not affect the original list
        copiedList.add("Date");
        System.out.println("\nAfter adding 'Date' to copiedList:");
        System.out.println("Original List: " + originalList);
        System.out.println("Copied List:   " + copiedList);
    }
}

Creating an Immutable List (Java 9+)

Sometimes you need a list that cannot be changed after creation. Java 9 introduced a convenient way to create small, immutable lists.

Syntax: List<Type> listName = List.of(element1, element2, ...);

Important Notes:

  • This creates an immutable list. You cannot add, remove, or change elements.
  • The list can contain null elements.
  • This method is part of the List interface, not the ArrayList class. The returned type is List, but it's a specialized, immutable implementation.

Example:

import java.util.List;
public class Main {
    public static void main(String[] args) {
        // Create an immutable list
        List<String> daysOfWeek = List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
        System.out.println("Immutable List: " + daysOfWeek);
        // This will throw an UnsupportedOperationException
        // daysOfWeek.add("Funday"); 
    }
}

Key Differences and Best Practices

Method Type Mutability When to Use
new ArrayList<>() Mutable Yes The default choice. Use when you need a list that you will add to, remove from, or modify.
new ArrayList<>(capacity) Mutable Yes Use when you know the approximate size of the list to optimize performance and avoid resizing.
new ArrayList<>(collection) Mutable Yes Use when you need a mutable copy of an existing collection.
List.of(...) Immutable No Use for fixed, unchanging sets of data, like configuration values, days of the week, or method return values where you don't want the caller to modify the list.

A Quick Note on List vs. ArrayList

It's a best practice to program to an interface, not an implementation. This means you should usually declare your list variable as List rather than ArrayList.

Good Practice (Recommended):

import java.util.List;
import java.util.ArrayList;
// Declare the variable as the interface List
List<String> names = new ArrayList<>();

Why?

  • Flexibility: If you later decide that a LinkedList would be a better data structure for your use case, you only need to change the one line of code where you create the instance. The rest of your code that uses the names variable remains unchanged.
  • Abstraction: You are focusing on the what (you need a list of strings) rather than the how (you need an ArrayList of strings).

Summary

For most day-to-day Java programming, you will use:

// 1. Import the class
import java.util.ArrayList;
// 2. Declare and initialize (the recommended way)
List<String> myList = new ArrayList<>(); 
// Or, if you specifically need the ArrayList type for some reason:
ArrayList<String> myArrayList = new ArrayList<>();
分享:
扫描分享到社交APP
上一篇
下一篇