You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

初始化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 both null—since there are no elements in the list yet
  • The size field (which tracks element count) is set to 0
  • 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 size will 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:26:11