java linkedlist wait_Java LinkedList

本文详细介绍了LinkedList的数据结构特点,包括其基于双向链表实现的原理、构造方法、常用操作方法及其时间复杂度特性。对比ArrayList,LinkedList在增加和删除操作上表现更优。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LinkedList是基于双向链表实现的,先看构造方法和方法

Constructor Summary

Constructors

ConstructorDescription

Constructs an empty list.

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Method Summary

Modifier and TypeMethodDescription

void

add​(int index,E element)

Inserts the specified element at the specified position in this list.

boolean

add​(E e)

Appends the specified element to the end of this list.

boolean

addAll​(int index,Collection extendsE> c)

Inserts all of the elements in the specified collection into this list, starting at the specified position.

boolean

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

void

Inserts the specified element at the beginning of this list.

void

Appends the specified element to the end of this list.

void

Removes all of the elements from this list.

Returns a shallow copy of this LinkedList.

boolean

Returns true if this list contains the specified element.

Returns an iterator over the elements in this deque in reverse sequential order.

Retrieves, but does not remove, the head (first element) of this list.

get​(int index)

Returns the element at the specified position in this list.

Returns the first element in this list.

Returns the last element in this list.

int

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

int

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

listIterator​(int index)

Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

boolean

Adds the specified element as the tail (last element) of this list.

boolean

Inserts the specified element at the front of this list.

boolean

Inserts the specified element at the end of this list.

Retrieves, but does not remove, the head (first element) of this list.

Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.

Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

Retrieves and removes the head (first element) of this list.

Retrieves and removes the first element of this list, or returns null if this list is empty.

Retrieves and removes the last element of this list, or returns null if this list is empty.

Pops an element from the stack represented by this list.

void

Pushes an element onto the stack represented by this list.

Retrieves and removes the head (first element) of this list.

remove​(int index)

Removes the element at the specified position in this list.

boolean

Removes the first occurrence of the specified element from this list, if it is present.

Removes and returns the first element from this list.

boolean

Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).

Removes and returns the last element from this list.

boolean

Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).

set​(int index,E element)

Replaces the element at the specified position in this list with the specified element.

int

Returns the number of elements in this list.

Creates a fail-fast Spliterator over the elements in this list.

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

 T[]

toArray​(T[] a)

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array

构造方法LinkedList()//创建一个空LinkedList

LinkedList(Coolection extends E>)//创建一个有内容的LinkedList,可以将别的LinkedList复制进来(不是引用)

LinkedList增加和删除元素都是常数时间(假设位置已知)

add(int index,E element)向索引位置插入,线性时间

add(E e)默认队尾

addFirst(E e)插入队头

addList(E e)插入队尾

removeFirst(E e)删除队头元素

removeLast(E e)删除队尾元素

LinkedList查询修改元素是线性时间,这点不如ArrayList但是LinkedList的增加和删除是常数时间,不需要移动其他元素的位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值