Java集合框架学习(六) LinkedList详解
LinkedList介绍
ArrayList与LinkedList都是List接口的实现类,因此都实现了List的所有未实现的方法,只是实现的方式有所不同。
LinkedList是采用链表的方式来实现List接口的,它本身有自己特定的方法,如: addFirst(),addLast(),getFirst(),removeFirst()等.。
由于是采用链表实现的,因此在进行insert和remove动作时在效率上要比ArrayList要好得多!适合用来实现Stack(堆栈)与
Queue(队列),前者先进后出,后者是先进先出。
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
例子介绍
package com.dylan.collection;
import java.util.LinkedList;
/**
* @author xusucheng
* @create 2018-01-30
**/
public class LinkedListExample {
public static void main(String args[]) {
/* Linked List Declaration */
LinkedList<String> linkedlist = new LinkedList<String>();
/*add(String Element) is used for adding
* the elements to the linked list*/
linkedlist.add("Item1");
linkedlist.add("Item5");
linkedlist.add("Item3");
linkedlist.add("Item6");
linkedlist.add("Item2");
/*Display Linked List Content*/
System.out.println("Linked List Content: " +linkedlist);
/*Add First and Last Element*/
linkedlist.addFirst("First Item");
linkedlist.addLast("Last Item");
System.out.println("LinkedList Content after addition: " +linkedlist);
/*This is how to get and set Values*/
Object firstvar = linkedlist.get(0);
System.out.println("First element: " +firstvar);
linkedlist.set(0, "Changed first item");
Object firstvar2 = linkedlist.get(0);
System.out.println("First element after update by set method: " +firstvar2);
/*Remove first and last element*/
linkedlist.removeFirst();
linkedlist.removeLast();
System.out.println("LinkedList after deletion of first and last element: " +linkedlist);
/* Add to a Position and remove from a position*/
linkedlist.add(0, "Newly added item");
linkedlist.remove(2);
System.out.println("Final Content: " +linkedlist);
}
}
常用方法
增 : add, addAll, addFirst, addLast, push删: clear, removeFirst, removeLast
改: set,
查: size, get, getFirst, getLast, indexOf, lastIndexOf, peek,
转换:toArray
如何遍历
1.For循环方式2.高级For循环(foreach)
3.Iterator迭代器
4.While循环方式
package com.dylan.collection;
import java.util.Iterator;
import java.util.LinkedList;
/**
* @author xusucheng
* @create 2018-01-30
**/
public class LinkedListLoopDemo {
public static void main(String args[]) {
/*LinkedList declaration*/
LinkedList<String> linkedlist=new LinkedList<String>();
linkedlist.add("Apple");
linkedlist.add("Orange");
linkedlist.add("Mango");
/*for loop*/
System.out.println("**For loop**");
for(int num=0; num<linkedlist.size(); num++)
{
System.out.println(linkedlist.get(num));
}
/*Advanced for loop*/
System.out.println("**Advanced For loop**");
for(String str: linkedlist)
{
System.out.println(str);
}
/*Using Iterator*/
System.out.println("**Iterator**");
Iterator i = linkedlist.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
/* Using While Loop*/
System.out.println("**While Loop**");
int num = 0;
while (linkedlist.size() > num) {
System.out.println(linkedlist.get(num));
num++;
}
}
}
模拟栈操作
package com.dylan.collection;
import java.util.LinkedList;
/**
* @author xusucheng
* @create 2018-01-30
**/
public class LinkedListPushPopDemo {
public static void main(String[] args) {
// Create a LinkedList of Strings
LinkedList<String> list = new LinkedList<String>();
// Add few Elements
list.add("Jack");
list.add("Robert");
list.add("Chaitanya");
list.add("kate");
// Display LinkList elements
System.out.println("LinkedList contains: "+list);
// push Element the list
list.push("NEW ELEMENT");
// Display after push operation
System.out.println("LinkedList contains: "+list);
}
}
转换LinkedList为ArrayList
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ConvertExample {
public static void main(String[] args) {
LinkedList<String> linkedlist = new LinkedList<String>();
linkedlist.add("Harry");
linkedlist.add("Jack");
linkedlist.add("Tim");
linkedlist.add("Rick");
linkedlist.add("Rock");
List<String> list = new ArrayList<String>(linkedlist);
for (String str : list){
System.out.println(str);
}
}
}
转换LinkedList为Array
import java.util.LinkedList;
public class ConvertExample {
public static void main(String[] args) {
//Creating and populating LinkedList
LinkedList<String> linkedlist = new LinkedList<String>();
linkedlist.add("Harry");
linkedlist.add("Maddy");
linkedlist.add("Chetan");
linkedlist.add("Chauhan");
linkedlist.add("Singh");
//Converting LinkedList to Array
String[] array = linkedlist.toArray(new String[linkedlist.size()]);
//Displaying Array content
System.out.println("Array Elements:");
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
}
}