List list =java new listnkedList(); 为什么list不能直接用list.addFirst("")调用LinkedList里面的addFirst

 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
存储学生的基本信息
下载积分:3000
内容提示:存储学生的基本信息
文档格式:PPT|
浏览次数:111|
上传日期: 17:29:40|
文档星级:
全文阅读已结束,如果下载本文需要使用
 3000 积分
下载此文档
该用户还上传了这些文档
存储学生的基本信息
关注微信公众号今天看啥 热点:
java LinkedList的使用方法介绍,javalinkedlist
LinkedList类是双向列表,列表中的每个节点都包含了对前一个和后一个元素的引用.
LinkedList的构造函数如下
1. public LinkedList(): &——生成空的链表
2. public LinkedList(Collection col): &复制构造函数
1、获取链表的第一个和最后一个元素
[java]&view
import&java.util.LinkedL&&
public&class&LinkedListTest{&&
&&public&static&void&main(String[]&args)&{&&
&&&&LinkedList&String&&lList&=&new&LinkedList&String&();&&
&&&&lList.add(&1&);&&
&&&&lList.add(&2&);&&
&&&&lList.add(&3&);&&
&&&&lList.add(&4&);&&
&&&&lList.add(&5&);&&
&&&&System.out.println(&链表的第一个元素是&:&&&+&lList.getFirst());&&
&&&&System.out.println(&链表最后一个元素是&:&&&+&lList.getLast());&&
2、获取链表元素&&
[java]&view
for&(String&str:&lList)&{&&
&&&&&&System.out.println(str);&&
3、从链表生成子表
[java]&view
List&subl&=&lList.subList(1,&4);&&
&&&&System.out.println(subl);&&
&&&&lst.remove(2);&&
&&&&System.out.println(lst);&&
&&&&System.out.println(lList);&&
4、添加元素:添加单个元素
&如果不指定索引的话,元素将被添加到链表的最后.
public boolean add(Object element)
public boolean add(int index, Object element)
也可以把链表当初栈或者队列来处理:
public boolean addFirst(Object element)
public boolean addLast(Object element)
addLast()方法和不带索引的add()方法实现的效果一样.
[java]&view
import&java.util.LinkedL&&
public&class&LinkedListTest{&&
&&public&static&void&main(String[]&a)&{&&
&&&&LinkedList&list&=&new&LinkedList();&&
&&&&list.add(&A&);&&
&&&&list.add(&B&);&&
&&&&list.add(&C&);&&
&&&&list.add(&D&);&&
&&&&list.addFirst(&X&);&&
&&&&list.addLast(&Z&);&&
&&&&System.out.println(list);&&
5、删除元素
[java]&view
public&Object&removeFirst()&&
public&Object&removeLast()&&
import&java.util.LinkedL&&
public&class&MainClass&{&&
&&public&static&void&main(String[]&a)&{&&
&&&&LinkedList&list&=&new&LinkedList();&&
&&&&list.add(&A&);&&
&&&&list.add(&B&);&&
&&&&list.add(&C&);&&
&&&&list.add(&D&);&&
&&&&list.removeFirst();&&
&&&&list.removeLast();&&
&&&&System.out.println(list);&&
6、使用链表实现栈效果
[java]&view
import&java.util.LinkedL&&
public&class&MainClass&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&StackL&stack&=&new&StackL();&&
&&&&for&(int&i&=&0;&i&&&10;&i++)&&
&&&&&&stack.push(i);&&
&&&&System.out.println(stack.top());&&
&&&&System.out.println(stack.top());&&
&&&&System.out.println(stack.pop());&&
&&&&System.out.println(stack.pop());&&
&&&&System.out.println(stack.pop());&&
class&StackL&{&&
&&private&LinkedList&list&=&new&LinkedList();&&
&&public&void&push(Object&v)&{&&
&&&&list.addFirst(v);&&
&&public&Object&top()&{&&
&&&&return&list.getFirst();&&
&&public&Object&pop()&{&&
&&&&return&list.removeFirst();&&
7、使用链表来实现队列效果
[java]&view
import&java.util.LinkedL&&
public&class&MainClass&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&Queue&queue&=&new&Queue();&&
&&&&for&(int&i&=&0;&i&&&10;&i++)&&
&&&&&&queue.put(Integer.toString(i));&&
&&&&while&(!queue.isEmpty())&&
&&&&&&System.out.println(queue.get());&&
class&Queue&{&&
&&private&LinkedList&list&=&new&LinkedList();&&
&&public&void&put(Object&v)&{&&
&&&&list.addFirst(v);&&
&&public&Object&get()&{&&
&&&&return&list.removeLast();&&
&&public&boolean&isEmpty()&{&&
&&&&return&list.isEmpty();&&
8、将LinkedList转换成ArrayList
[java]&view
ArrayList&String&&arrayList&=&new&ArrayList&String&(linkedList);&&
&&&&for&(String&s&:&arrayList)&{&&
&&&&&&System.out.println(&s&=&&&+&s);&&
9、删掉所有元素:清空LinkedList
& & lList.clear();
10、删除列表的首位元素
[java]&view
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&LinkedList&String&&lList&=&new&LinkedList&String&();&&
&&&&lList.add(&1&);&&
&&&&lList.add(&2&);&&
&&&&lList.add(&3&);&&
&&&&lList.add(&4&);&&
&&&&lList.add(&5&);&&
&&&&System.out.println(lList);&&
&&&&&&&&&&
&&&&Object&object&=&lList.removeFirst();&&
&&&&System.out.println(object&+&&&has&been&removed&);&&
&&&&System.out.println(lList);&&
&&&&object&=&lList.removeLast();&&
&&&&System.out.println(object&+&&&has&been&removed&);&&
&&&&System.out.println(lList);&&
11、根据范围删除列表元素
[java]&view
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&LinkedList&String&&lList&=&new&LinkedList&String&();&&
&&&&lList.add(&1&);&&
&&&&lList.add(&2&);&&
&&&&lList.add(&3&);&&
&&&&lList.add(&4&);&&
&&&&lList.add(&5&);&&
&&&&System.out.println(lList);&&
&&&&lList.subList(2,&5).clear();&&
&&&&System.out.println(lList);&&
12、删除链表的特定元素
[java]&view
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&LinkedList&String&&lList&=&new&LinkedList&String&();&&
&&&&lList.add(&1&);&&
&&&&lList.add(&2&);&&
&&&&lList.add(&3&);&&
&&&&lList.add(&4&);&&
&&&&lList.add(&5&);&&
&&&&System.out.println(lList);&&
&&&&System.out.println(lList.remove(&2&));&&
&&&&System.out.println(lList);&&
&&&&Object&obj&=&lList.remove(2);&&&&
&&&&System.out.println(obj&+&&&已经从链表删除&);&&
&&&&System.out.println(lList);&&
13、将LinkedList转换为数组,数组长度为0
[java]&view
import&java.util.LinkedL&&
import&java.util.L&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&List&String&&theList&=&new&LinkedList&String&();&&
&&&&theList.add(&A&);&&
&&&&theList.add(&B&);&&
&&&&theList.add(&C&);&&
&&&&theList.add(&D&);&&
&&&&String[]&my&=&theList.toArray(new&String[0]);&&
&&&&for&(int&i&=&0;&i&&&my.&i++)&{&&
&&&&&&System.out.println(my[i]);&&
14、将LinkedList转换为数组,数组长度为链表长度
[java]&view
import&java.util.LinkedL&&
import&java.util.L&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&List&String&&theList&=&new&LinkedList&String&();&&
&&&&theList.add(&A&);&&
&&&&theList.add(&B&);&&
&&&&theList.add(&C&);&&
&&&&theList.add(&D&);&&
&&&&String[]&my&=&theList.toArray(new&String[theList.size()]);&&
&&&&for&(int&i&=&0;&i&&&my.&i++)&{&&
&&&&&&System.out.println(my[i]);&&
15、将LinkedList转换成ArrayList
[java]&view
import&java.util.ArrayL&&
import&java.util.LinkedL&&
import&java.util.L&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&LinkedList&String&&myQueue&=&new&LinkedList&String&();&&
&&&&myQueue.add(&A&);&&
&&&&myQueue.add(&B&);&&
&&&&myQueue.add(&C&);&&
&&&&myQueue.add(&D&);&&
&&&&List&String&&myList&=&new&ArrayList&String&(myQueue);&&
&&&&for&(Object&theFruit&:&myList)&&
&&&&&&System.out.println(theFruit);&&
16、实现栈
[java]&view
import&java.util.C&&
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&argv)&throws&Exception&{&&
&&&&LinkedList&stack&=&new&LinkedList();&&
&&&&Object&object&=&&&;&&
&&&&stack.addFirst(object);&&
&&&&Object&o&=&stack.getFirst();&&
&&&&stack&=&(LinkedList)&Collections.synchronizedList(stack);&&
17、实现队列
[java]&view
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&argv)&throws&Exception&{&&
&&&&LinkedList&queue&=&new&LinkedList();&&
&&&&Object&object&=&&&;&&
&&&&queue.add(object);&&
&&&&Object&o&=&queue.removeFirst();&&
18 、同步方法
[java]&view
import&java.util.C&&
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&argv)&throws&Exception&{&&
&&&&LinkedList&queue&=&new&LinkedList();&&
&&&&Object&object&=&&&;&&
&&&&queue.add(object);&&
&&&&Object&o&=&queue.removeFirst();&&
&&&&queue&=&(LinkedList)&Collections.synchronizedList(queue);&&
19、查找元素位置
[java]&view
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&LinkedList&String&&lList&=&new&LinkedList&String&();&&
&&&&lList.add(&1&);&&
&&&&lList.add(&2&);&&
&&&&lList.add(&3&);&&
&&&&lList.add(&4&);&&
&&&&lList.add(&5&);&&
&&&&lList.add(&2&);&&
&&&&System.out.println(lList.indexOf(&2&));&&
&&&&System.out.println(lList.lastIndexOf(&2&));&&
20、替换元素
[java]&view
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&LinkedList&String&&lList&=&new&LinkedList&String&();&&
&&&&lList.add(&1&);&&
&&&&lList.add(&2&);&&
&&&&lList.add(&3&);&&
&&&&lList.add(&4&);&&
&&&&lList.add(&5&);&&
&&&&System.out.println(lList);&&
&&&&lList.set(3,&&Replaced&);&&
&&&&System.out.println(lList);&&
21、链表添加对象
[java]&view
import&java.util.LinkedL&&
class&Address&{&&
&&private&String&&&
&&private&String&&&
&&private&String&&&
&&private&String&&&
&&private&String&&&
&&Address(String&n,&String&s,&String&c,&String&st,&String&cd)&{&&
&&&&name&=&n;&&
&&&&street&=&s;&&
&&&&city&=&c;&&
&&&&state&=&&&
&&&&code&=&&&
&&public&String&toString()&{&&
&&&&return&name&+&&&&&+&street&+&&&&&+&city&+&&&&&+&state&+&&&&&+&&&
class&MailList&{&&
&&public&static&void&main(String&args[])&{&&
&&&&LinkedList&Address&&ml&=&new&LinkedList&Address&();&&
&&&&ml.add(new&Address(&A&,&&11&Ave&,&&U&,&&IL&,&&11111&));&&
&&&&ml.add(new&Address(&R&,&&11&Lane&,&&M&,&&IL&,&&22222&));&&
&&&&ml.add(new&Address(&T&,&&8&St&,&&C&,&&IL&,&&33333&));&&
&&&&for&(Address&element&:&ml)&&
&&&&&&System.out.println(element&+&&\n&);&&
22、确认链表是否存在特定元素
[java]&view
import&java.util.LinkedL&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&{&&
&&&&LinkedList&String&&lList&=&new&LinkedList&String&();&&
&&&&lList.add(&1&);&&
&&&&lList.add(&2&);&&
&&&&lList.add(&3&);&&
&&&&lList.add(&4&);&&
&&&&lList.add(&5&);&&
&&&&if&(lList.contains(&4&))&{&&
&&&&&&System.out.println(&LinkedList&contains&4&);&&
&&&&}&else&{&&
&&&&&&System.out.println(&LinkedList&does&not&contain&4&);&&
23、根据链表元素生成对象数组
[java]&view
Object[]&objArray&=&lList.toArray();&&
for&(Object&obj:&objArray)&{&&
&&&System.out.println(obj);&&
24、链表多线程
[java]&view
import&java.util.C&&
import&java.util.LinkedL&&
import&java.util.L&&
class&PrepareProduction&implements&Runnable&{&&
&&private&final&List&String&&&&
&&PrepareProduction(List&String&&q)&{&&
&&&&queue&=&q;&&
&&public&void&run()&{&&
&&&&queue.add(&1&);&&
&&&&queue.add(&done&);&&
class&DoProduction&implements&Runnable&{&&
&&private&final&List&String&&&&
&&DoProduction(List&String&&q)&{&&
&&&&queue&=&q;&&
&&public&void&run()&{&&
&&&&String&value&=&queue.remove(0);&&
&&&&while&(!value.equals(&*&))&{&&
&&&&&&System.out.println(value);&&
&&&&&&value&=&queue.remove(0);&&
public&class&Main&{&&
&&public&static&void&main(String[]&args)&throws&Exception&{&&
&&&&List&q&=&Collections.synchronizedList(new&LinkedList&String&());&&
&&&&Thread&p1&=&new&Thread(new&PrepareProduction(q));&&
&&&&Thread&c1&=&new&Thread(new&DoProduction(q));&&
&&&&p1.start();&&
&&&&c1.start();&&
&&&&p1.join();&&
&&&&c1.join();&&
25、优先级链表(来自JBOSS)
[java]&view
import&java.util.ArrayL&&
import&java.util.LinkedL&&
import&java.util.L&&
import&java.util.ListI&&
import&java.util.NoSuchElementE&&
public&class&BasicPriorityLinkedList&{&&
&&protected&LinkedList[]&linkedL&&
&&protected&int&&&
&&protected&int&&&
&&public&BasicPriorityLinkedList(int&priorities)&{&&
&&&&this.priorities&=&&&
&&&&initDeques();&&
&&public&void&addFirst(Object&obj,&int&priority)&{&&
&&&&linkedLists[priority].addFirst(obj);&&
&&&&size++;&&
&&public&void&addLast(Object&obj,&int&priority)&{&&
&&&&linkedLists[priority].addLast(obj);&&
&&&&size++;&&
&&public&Object&removeFirst()&{&&
&&&&Object&obj&=&null;&&
&&&&for&(int&i&=&priorities&-&1;&i&&=&0;&i--)&{&&
&&&&&&LinkedList&ll&=&linkedLists[i];&&
&&&&&&if&(!ll.isEmpty())&{&&
&&&&&&&&obj&=&ll.removeFirst();&&
&&&&&&&&break;&&
&&&&if&(obj&!=&null)&{&&
&&&&&&size--;&&
&&&&return&&&
&&public&Object&removeLast()&{&&
&&&&Object&obj&=&null;&&
&&&&for&(int&i&=&0;&i&&&&i++)&{&&
&&&&&&LinkedList&ll&=&linkedLists[i];&&
&&&&&&if&(!ll.isEmpty())&{&&
&&&&&&&&obj&=&ll.removeLast();&&
&&&&&&if&(obj&!=&null)&{&&
&&&&&&&&break;&&
&&&&if&(obj&!=&null)&{&&
&&&&&&size--;&&
&&&&return&&&
&&public&Object&peekFirst()&{&&
&&&&Object&obj&=&null;&&
&&&&for&(int&i&=&priorities&-&1;&i&&=&0;&i--)&{&&
&&&&&&LinkedList&ll&=&linkedLists[i];&&
&&&&&&if&(!ll.isEmpty())&{&&
&&&&&&&&obj&=&ll.getFirst();&&
&&&&&&if&(obj&!=&null)&{&&
&&&&&&&&break;&&
&&&&return&&&
&&public&List&getAll()&{&&
&&&&List&all&=&new&ArrayList();&&
&&&&for&(int&i&=&priorities&-&1;&i&&=&0;&i--)&{&&
&&&&&&LinkedList&deque&=&linkedLists[i];&&
&&&&&&all.addAll(deque);&&
&&&&return&&&
&&public&void&clear()&{&&
&&&&initDeques();&&
&&public&int&size()&{&&
&&&&return&&&
&&public&boolean&isEmpty()&{&&
&&&&return&size&==&0;&&
&&public&ListIterator&iterator()&{&&
&&&&return&new&PriorityLinkedListIterator(linkedLists);&&
&&protected&void&initDeques()&{&&
&&&&linkedLists&=&new&LinkedList[priorities];&&
&&&&for&(int&i&=&0;&i&&&&i++)&{&&
&&&&&&linkedLists[i]&=&new&LinkedList();&&
&&&&size&=&0;&&
&&class&PriorityLinkedListIterator&implements&ListIterator&{&&
&&&&private&LinkedList[]&&&
&&&&private&int&&&
&&&&private&ListIterator&currentI&&
&&&&PriorityLinkedListIterator(LinkedList[]&lists)&{&&
&&&&&&this.lists&=&&&
&&&&&&index&=&lists.length&-&1;&&
&&&&&&currentIter&=&lists[index].listIterator();&&
&&&&public&void&add(Object&arg0)&{&&
&&&&&&throw&new&UnsupportedOperationException();&&
&&&&public&boolean&hasNext()&{&&
&&&&&&if&(currentIter.hasNext())&{&&
&&&&&&&&return&true;&&
&&&&&&while&(index&&=&0)&{&&
&&&&&&&&if&(index&==&0&||&currentIter.hasNext())&{&&
&&&&&&&&&&break;&&
&&&&&&&&}&&
&&&&&&&&index--;&&
&&&&&&&&currentIter&=&lists[index].listIterator();&&
&&&&&&return&currentIter.hasNext();&&
&&&&public&boolean&hasPrevious()&{&&
&&&&&&throw&new&UnsupportedOperationException();&&
&&&&public&Object&next()&{&&
&&&&&&if&(!hasNext())&{&&
&&&&&&&&throw&new&NoSuchElementException();&&
&&&&&&return&currentIter.next();&&
&&&&public&int&nextIndex()&{&&
&&&&&&throw&new&UnsupportedOperationException();&&
&&&&public&Object&previous()&{&&
&&&&&&throw&new&UnsupportedOperationException();&&
&&&&public&int&previousIndex()&{&&
&&&&&&throw&new&UnsupportedOperationException();&&
&&&&public&void&remove()&{&&
&&&&&&currentIter.remove();&&
&&&&&&size--;&&
&&&&public&void&set(Object&obj)&{&&
&&&&&&throw&new&UnsupportedOperationException();&&
26、生成list的帮助类(来自google)
[java]&view
import&java.util.ArrayL&&
import&java.util.C&&
import&java.util.LinkedL&&
import&java.util.L&&
public&class&Lists&{&&
&&private&Lists()&{&}&&
&&public&static&&E&&ArrayList&E&&newArrayList()&{&&
&&&&return&new&ArrayList&E&();&&
&&public&static&&E&&ArrayList&E&&newArrayListWithCapacity(int&initialCapacity)&{&&
&&&&return&new&ArrayList&E&(initialCapacity);&&
&&public&static&&E&&ArrayList&E&&newArrayList(E...&elements)&{&&
&&&&ArrayList&E&&set&=&newArrayList();&&
&&&&Collections.addAll(set,&elements);&&
&&&&return&&&
&&public&static&&E&&ArrayList&E&&newArrayList(Iterable&?&extends&E&&elements)&{&&
&&&&ArrayList&E&&list&=&newArrayList();&&
&&&&for(E&e&:&elements)&{&&
&&&&&&list.add(e);&&
&&&&return&&&
&&public&static&&E&&LinkedList&E&&newLinkedList()&{&&
&&&&return&new&LinkedList&E&();&&
相关搜索:
相关阅读:
相关频道:
&&&&&&&&&&&&&&&&
WEB编程教程最近更新LinkedList (Java Platform SE 7 )
JavaScript is disabled on your browser.
Class LinkedList&E&
java.util.LinkedList&E&
Type Parameters:E - the type of elements held in this collection
All Implemented Interfaces:
, , &E&, &E&, &E&, &E&, &E&
public class LinkedList&E&
extends &E&
implements &E&, &E&, ,
Doubly-linked list implementation of the List and Deque
interfaces.
Implements all optional list operations, and permits all
elements (including null).
All of the operations perform as could be expected for a doubly-linked
Operations that index into the list will traverse the list from
the beginning or the end, whichever is closer to the specified index.
Note that this implementation is not synchronized.
If multiple threads access a linked list concurrently, and at least
one of the threads modifies the list structurally, it must be
synchronized externally.
(A structural modification is any operation
that adds or deletes
merely setting the value of
an element is not a structural modification.)
This is typically
accomplished by synchronizing on some object that naturally
encapsulates the list.
If no such object exists, the list should be "wrapped" using the
This is best done at creation time, to prevent accidental
unsynchronized access to the list:
List list = Collections.synchronizedList(new LinkedList(...));
The iterators returned by this class's iterator and
listIterator methods are fail-fast: if the list is
structurally modified at any time after the iterator is created, in
any way except through the Iterator's own remove or
add methods, the iterator will throw a .
Thus, in the face of concurrent
modification, the iterator fails quickly and cleanly, rather than
risking arbitrary, non-deterministic behavior at an undetermined
time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed
as it is, generally speaking, impossible to make any hard guarantees in the
presence of unsynchronized concurrent modification.
Fail-fast iterators
throw ConcurrentModificationException on a best-effort basis.
Therefore, it would be wrong to write a program that depended on this
exception for its correctness:
the fail-fast behavior of iterators
should be used only to detect bugs.
This class is a member of the
See Also:,
Field Summary
Fields inherited from class&java.util.
Constructor Summary
Constructors&
Constructor and Description
Constructs an empty list.
(&? extends &&c)
Constructs a list containing the elements of the specified
collection, in the order they are returned by the collection's
Method Summary
Modifier and Type
Method and Description
Appends the specified element to the end of this list.
(int&index,
Inserts the specified element at the specified position in this list.
(&? extends &&c)
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.
(int&index,
&? extends &&c)
Inserts all of the elements in the specified collection into this
list, starting at the specified position.
Inserts the specified element at the beginning of this list.
Appends the specified element to the end of this list.
Removes all of the elements from this list.
Returns a shallow copy of this LinkedList.
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.
(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.
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
(int&index)
Returns a list-iterator of the elements in this list (in proper
sequence), starting at the specified position in the list.
Adds the specified element as the tail (last element) of this list.
Inserts the specified element at the front of this list.
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.
Pushes an element onto the stack represented by this list.
Retrieves and removes the head (first element) of this list.
(int&index)
Removes the element at the specified position in this list.
Removes the first occurrence of the specified element from this list,
if it is present.
Removes and returns the first element from this list.
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.
Removes the last occurrence of the specified element in this
list (when traversing the list from head to tail).
(int&index,
Replaces the element at the specified position in this list with the
specified element.
Returns the number of elements in this list.
Returns an array containing all of the elements in this list
in proper sequence (from first to last element).
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.
Methods inherited from class&java.util.
Methods inherited from class&java.util.
Methods inherited from class&java.util.
Methods inherited from class&java.lang.
, , , , , ,
Methods inherited from interface&java.util.
, , , , , , , ,
Methods inherited from interface&java.util.
Constructor Detail
LinkedList
public&LinkedList()
Constructs an empty list.
LinkedList
public&LinkedList(&? extends &&c)
Constructs a list containing the elements of the specified
collection, in the order they are returned by the collection's
Parameters:c - the collection whose elements are to be placed into this list
- if the specified collection is null
Method Detail
public&&getFirst()
Returns the first element in this list.
Specified by:
&in interface&&&
Returns:the first element in this list
- if this list is empty
public&&getLast()
Returns the last element in this list.
Specified by:
&in interface&&&
Returns:the last element in this list
- if this list is empty
removeFirst
public&&removeFirst()
Removes and returns the first element from this list.
Specified by:
&in interface&&&
Returns:the first element from this list
- if this list is empty
removeLast
public&&removeLast()
Removes and returns the last element from this list.
Specified by:
&in interface&&&
Returns:the last element from this list
- if this list is empty
public&void&addFirst(&e)
Inserts the specified element at the beginning of this list.
Specified by:
&in interface&&&
Parameters:e - the element to add
public&void&addLast(&e)
Appends the specified element to the end of this list.
This method is equivalent to .
Specified by:
&in interface&&&
Parameters:e - the element to add
public&boolean&contains(&o)
Returns true if this list contains the specified element.
More formally, returns true if and only if this list contains
at least one element e such that
(o==null&?&e==null&:&o.equals(e)).
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:o - element whose presence in this list is to be tested
Returns:true if this list contains the specified element
public&int&size()
Returns the number of elements in this list.
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Specified by:
&in class&&&
Returns:the number of elements in this list
public&boolean&add(&e)
Appends the specified element to the end of this list.
This method is equivalent to .
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:e - element to be appended to this list
Returns:true (as specified by )
public&boolean&remove(&o)
Removes the first occurrence of the specified element from this list,
if it is present.
If this list does not contain the element, it is
unchanged.
More formally, removes the element with the lowest index
i such that
(o==null&?&get(i)==null&:&o.equals(get(i)))
(if such an element exists).
Returns true if this list
contained the specified element (or equivalently, if this list
changed as a result of the call).
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:o - element to be removed from this list, if present
Returns:true if this list contained the specified element
public&boolean&addAll(&? extends &&c)
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.
The behavior of this operation is undefined if
the specified collection is modified while the operation is in
(Note that this will occur if the specified collection is
this list, and it's nonempty.)
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:c - collection containing elements to be added to this list
Returns:true if this list changed as a result of the call
- if the specified collection is nullSee Also:
public&boolean&addAll(int&index,
&? extends &&c)
Inserts all of the elements in the specified collection into this
list, starting at the specified position.
Shifts the element
currently at that position (if any) and any subsequent elements to
the right (increases their indices).
The new elements will appear
in the list in the order that they are returned by the
specified collection's iterator.
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:index - index at which to insert the first element
from the specified collectionc - collection containing elements to be added to this list
Returns:true if this list changed as a result of the call
- if the index is out of range
(index & 0 || index & size())
- if the specified collection is null
public&void&clear()
Removes all of the elements from this list.
The list will be empty after this call returns.
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Overrides:
&in class&&&
public&&get(int&index)
Returns the element at the specified position in this list.
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:index - index of the element to return
Returns:the element at the specified position in this list
- if the index is out of range
(index & 0 || index &= size())
public&&set(int&index,
Replaces the element at the specified position in this list with the
specified element.
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:index - index of the element to replaceelement - element to be stored at the specified position
Returns:the element previously at the specified position
- if the index is out of range
(index & 0 || index &= size())
public&void&add(int&index,
Inserts the specified element at the specified position in this list.
Shifts the element currently at that position (if any) and any
subsequent elements to the right (adds one to their indices).
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:index - index at which the specified element is to be insertedelement - element to be inserted
- if the index is out of range
(index & 0 || index & size())
public&&remove(int&index)
Removes the element at the specified position in this list.
Shifts any
subsequent elements to the left (subtracts one from their indices).
Returns the element that was removed from the list.
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:index - the index of the element to be removed
Returns:the element previously at the specified position
- if the index is out of range
(index & 0 || index &= size())
public&int&indexOf(&o)
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
More formally, returns the lowest index i such that
(o==null&?&get(i)==null&:&o.equals(get(i))),
or -1 if there is no such index.
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:o - element to search for
Returns:the index of the first occurrence of the specified element in
this list, or -1 if this list does not contain the element
lastIndexOf
public&int&lastIndexOf(&o)
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
More formally, returns the highest index i such that
(o==null&?&get(i)==null&:&o.equals(get(i))),
or -1 if there is no such index.
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:o - element to search for
Returns:the index of the last occurrence of the specified element in
this list, or -1 if this list does not contain the element
public&&peek()
Retrieves, but does not remove, the head (first element) of this list.
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Returns:the head of this list, or null if this list is emptySince:
public&&element()
Retrieves, but does not remove, the head (first element) of this list.
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Returns:the head of this list
- if this list is emptySince:
public&&poll()
Retrieves and removes the head (first element) of this list.
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Returns:the head of this list, or null if this list is emptySince:
public&&remove()
Retrieves and removes the head (first element) of this list.
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Returns:the head of this list
- if this list is emptySince:
public&boolean&offer(&e)
Adds the specified element as the tail (last element) of this list.
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Parameters:e - the element to add
Returns:true (as specified by )Since:
offerFirst
public&boolean&offerFirst(&e)
Inserts the specified element at the front of this list.
Specified by:
&in interface&&&
Parameters:e - the element to insert
Returns:true (as specified by )Since:
public&boolean&offerLast(&e)
Inserts the specified element at the end of this list.
Specified by:
&in interface&&&
Parameters:e - the element to insert
Returns:true (as specified by )Since:
public&&peekFirst()
Retrieves, but does not remove, the first element of this list,
or returns null if this list is empty.
Specified by:
&in interface&&&
Returns:the first element of this list, or null
if this list is emptySince:
public&&peekLast()
Retrieves, but does not remove, the last element of this list,
or returns null if this list is empty.
Specified by:
&in interface&&&
Returns:the last element of this list, or null
if this list is emptySince:
public&&pollFirst()
Retrieves and removes the first element of this list,
or returns null if this list is empty.
Specified by:
&in interface&&&
Returns:the first element of this list, or null if
this list is emptySince:
public&&pollLast()
Retrieves and removes the last element of this list,
or returns null if this list is empty.
Specified by:
&in interface&&&
Returns:the last element of this list, or null if
this list is emptySince:
public&void&push(&e)
Pushes an element onto the stack represented by this list.
words, inserts the element at the front of this list.
This method is equivalent to .
Specified by:
&in interface&&&
Parameters:e - the element to pushSince:
public&&pop()
Pops an element from the stack represented by this list.
words, removes and returns the first element of this list.
This method is equivalent to .
Specified by:
&in interface&&&
Returns:the element at the front of this list (which is the top
of the stack represented by this list)
- if this list is emptySince:
removeFirstOccurrence
public&boolean&removeFirstOccurrence(&o)
Removes the first occurrence of the specified element in this
list (when traversing the list from head to tail).
If the list
does not contain the element, it is unchanged.
Specified by:
&in interface&&&
Parameters:o - element to be removed from this list, if present
Returns:true if the list contained the specified elementSince:
removeLastOccurrence
public&boolean&removeLastOccurrence(&o)
Removes the last occurrence of the specified element in this
list (when traversing the list from head to tail).
If the list
does not contain the element, it is unchanged.
Specified by:
&in interface&&&
Parameters:o - element to be removed from this list, if present
Returns:true if the list contained the specified elementSince:
listIterator
public&&&&listIterator(int&index)
Returns a list-iterator of the elements in this list (in proper
sequence), starting at the specified position in the list.
Obeys the general contract of List.listIterator(int).
The list-iterator is fail-fast: if the list is structurally
modified at any time after the Iterator is created, in any way except
through the list-iterator's own remove or add
methods, the list-iterator will throw a
ConcurrentModificationException.
Thus, in the face of
concurrent modification, the iterator fails quickly and cleanly, rather
than risking arbitrary, non-deterministic behavior at an undetermined
time in the future.
Specified by:
&in interface&&&
Specified by:
&in class&&&
Parameters:index - index of the first element to be returned from the
list-iterator (by a call to next)
Returns:a ListIterator of the elements in this list (in proper
sequence), starting at the specified position in the list
- if the index is out of range
(index & 0 || index & size())See Also:
descendingIterator
public&&&&descendingIterator()
Description copied from interface:&
Returns an iterator over the elements in this deque in reverse
sequential order.
The elements will be returned in order from
last (tail) to first (head).
Specified by:
&in interface&&&
Returns:an iterator over the elements in this deque in reverse
sequenceSince:
public&&clone()
Returns a shallow copy of this LinkedList. (The elements
themselves are not cloned.)
Overrides:
&in class&
Returns:a shallow copy of this LinkedList instanceSee Also:
public&[]&toArray()
Returns an array containing all of the elements in this list
in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are
maintained by this list.
(In other words, this method must allocate
a new array).
The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Overrides:
&in class&&&
Returns:an array containing all of the elements in this list
in proper sequenceSee Also:
public&&T&&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.
If the list fits
in the specified array, it is returned therein.
Otherwise, a new
array is allocated with the runtime type of the specified array and
the size of this list.
If the list fits in the specified array with room to spare (i.e.,
the array has more elements than the list), the element in the array
immediately following the end of the list is set to null.
(This is useful in determining the length of the list only if
the caller knows that the list does not contain any null elements.)
method, this method acts as bridge between
array-based and collection-based APIs.
Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a list known to contain only strings.
The following code can be used to dump the list into a newly
allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().
Specified by:
&in interface&&&
Specified by:
&in interface&&&
Overrides:
&in class&&&
Parameters:a - the array into which the elements of the list are to
be stored, otherwise, a new array of the
same runtime type is allocated for this purpose.
Returns:an array containing the elements of the list
- if the runtime type of the specified array
is not a supertype of the runtime type of every element in
- if the specified array is null
For further API reference and developer documentation, see . That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
© , Oracle and/or its affiliates.
All rights reserved. Use is subject to . Also see the .
Scripting on this page tracks web page traffic, but does not change the content in any way.}

我要回帖

更多关于 new list string 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信