初始化LinkedList时的默认值是什么?
LinkedList初始化的默认值详解
Hey George! Thanks for asking (and circling back) about what defaults you get when initializing a LinkedList—let’s break this down clearly based on how you create the list:
1. 无参构造器 (new LinkedList<>())
When you use the no-argument constructor, here’s the default state you’ll get:
- The internal head (
first) and tail (last) nodes are bothnull—since there are no elements in the list yet - The
sizefield (which tracks element count) is set to0 - Important callout: Unlike ArrayList, LinkedList doesn’t have an "initial capacity" because it’s a doubly linked list—there’s no pre-allocated array, just empty node pointers to start with. No default elements are added automatically; the list starts completely empty.
2. 传入集合的构造器 (new LinkedList<>(Collection<? extends E> c))
If you initialize the LinkedList with an existing collection (like an ArrayList or HashSet), there are no extra default values applied:
- The list will immediately contain all elements from the input collection, in their original order
- The
sizewill match the size of the input collection - The head and tail nodes will point to the first and last elements of the copied collection, respectively
Thanks again for reaching out with this question, George—I hope this clears up exactly what defaults you’re dealing with when setting up a LinkedList!
内容的提问来源于stack exchange,提问作者Georg
相关产品推荐
相关产品推荐

