java LinkedLis t的26种使用方法
LinkedList类是双向列表,列表中的每个节点都包含了对前一个和后一个元素的引用.
LinkedList的构造函数如下
1. public LinkedList(): ——生成空的链表
2. public LinkedList(Collection col): 复制构造函数
1、获取链表的第一个和最后一个元素
import java.util.LinkedList;
- 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、获取链表元素
for (String str: lList) {
- System.out.println(str);
- }
3、从链表生成子表
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()方法实现的效果一样.
import java.util.LinkedList;
- 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、删除元素
public Object removeFirst()
- public Object removeLast()
- import java.util.LinkedList;
- 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、使用链表实现栈效果
import java.util.LinkedList;
- 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、使用链表来实现队列效果
import java.util.LinkedList;
- 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
ArrayList<String> arrayList = new ArrayList<String>(linkedList);
- for (String s : arrayList) {
- System.out.println("s = " + s);
- }
9、删掉所有元素:清空LinkedList
lList.clear();
10、删除列表的首位元素
import java.util.LinkedList;
- 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、根据范围删除列表元素
import java.util.LinkedList;
- 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、删除链表的特定元素
import java.util.LinkedList;
- 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"));//删除元素值=2的元素
- System.out.println(lList);
- Object obj = lList.remove(2); //删除第二个元素
- System.out.println(obj + " 已经从链表删除");
- System.out.println(lList);
- }
- }
13、将LinkedList转换为数组,数组长度为0
import java.util.LinkedList;
- import java.util.List;
- 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.length; i++) {
- System.out.println(my[i]);
- }
- }
- }
14、将LinkedList转换为数组,数组长度为链表长度
import java.util.LinkedList;
- import java.util.List;
- 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.length; i++) {
- System.out.println(my[i]);
- }
- }
- }
15、将LinkedList转换成ArrayList
import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- 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、实现栈
import java.util.Collections;
- import java.util.LinkedList;
- 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、实现队列
import java.util.LinkedList;
- public class Main {
- public static void main(String[] argv) throws Exception {
- LinkedList queue = new LinkedList();
- Object object = "";
- // Add to end of queue
- queue.add(object);
- // Get head of queue
- Object o = queue.removeFirst();
- }
- }
18 、同步方法
import java.util.Collections;
- import java.util.LinkedList;
- 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、查找元素位置
import java.util.LinkedList;
- 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、替换元素
import java.util.LinkedList;
- 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");//使用set方法替换元素,方法的第一个参数是元素索引,后一个是替换值
- System.out.println(lList);
- }
- }
21、链表添加对象
import java.util.LinkedList;
- class Address {
- private String name;
- private String street;
- private String city;
- private String state;
- private String code;
- Address(String n, String s, String c, String st, String cd) {
- name = n;
- street = s;
- city = c;
- state = st;
- code = cd;
- }
- public String toString() {
- return name + " " + street + " " + city + " " + state + " " + code;
- }
- }
- 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、确认链表是否存在特定元素
import java.util.LinkedList;
- 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、根据链表元素生成对象数组
Object[] objArray = lList.toArray();
- for (Object obj: objArray) {
- System.out.println(obj);
- }
24、链表多线程
import java.util.Collections;
- import java.util.LinkedList;
- import java.util.List;
- class PrepareProduction implements Runnable {
- private final List<String> queue;
- PrepareProduction(List<String> q) {
- queue = q;
- }
- public void run() {
- queue.add("1");
- queue.add("done");
- }
- }
- class DoProduction implements Runnable {
- private final List<String> queue;
- 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)
import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.ListIterator;
- import java.util.NoSuchElementException;
- public class BasicPriorityLinkedList {
- protected LinkedList[] linkedLists;
- protected int priorities;
- protected int size;
- public BasicPriorityLinkedList(int priorities) {
- this.priorities = 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 obj;
- }
- public Object removeLast() {
- Object obj = null;
- for (int i = 0; i < priorities; i++) {
- LinkedList ll = linkedLists[i];
- if (!ll.isEmpty()) {
- obj = ll.removeLast();
- }
- if (obj != null) {
- break;
- }
- }
- if (obj != null) {
- size--;
- }
- return obj;
- }
- 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 obj;
- }
- public List getAll() {
- List all = new ArrayList();
- for (int i = priorities - 1; i >= 0; i--) {
- LinkedList deque = linkedLists[i];
- all.addAll(deque);
- }
- return all;
- }
- public void clear() {
- initDeques();
- }
- public int size() {
- return size;
- }
- 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 < priorities; i++) {
- linkedLists[i] = new LinkedList();
- }
- size = 0;
- }
- class PriorityLinkedListIterator implements ListIterator {
- private LinkedList[] lists;
- private int index;
- private ListIterator currentIter;
- PriorityLinkedListIterator(LinkedList[] lists) {
- this.lists = 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)
import java.util.ArrayList;
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.List;
- 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 set;
- }
- public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
- ArrayList<E> list = newArrayList();
- for(E e : elements) {
- list.add(e);
- }
- return list;
- }
- public static <E> LinkedList<E> newLinkedList() {
- return new LinkedList<E>();
- }
- }
- 转至 :http://blog.csdn.net/jdsjlzx/article/details/41654295
java LinkedLis t的26种使用方法的更多相关文章
- java单例的几种实现方法
java单例的几种实现方法: 方式1: public class Something { private Something() {} private static class LazyHolder ...
- Java数据库连接池的几种配置方法(以MySQL数据库为例)
Java数据库连接池的几种配置方法(以MySQL数据库为例) 一.Tomcat配置数据源: 前提:需要将连接MySQL数据库驱动jar包放进Tomcat安装目录中common文件夹下的lib目录中 1 ...
- SuperDiamond在JAVA项目中的三种应用方法实践总结
SuperDiamond在JAVA项目中的三种应用方法实践总结 1.直接读取如下: @Test public static void test_simple(){ PropertiesConfigur ...
- Java中Map的三种遍历方法
Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...
- java数组中的三种排序方法中的冒泡排序方法
我记得我大学学java的时候,怎么就是搞不明白这三种排序方法,也一直不会,现在我有发过来学习下这三种方法并记录下来. 首先说说冒泡排序方法:冒泡排序方法就是把数组中的每一个元素进行比较,如果第i个元素 ...
- java实现定时任务的三种实现方法
转载于:https://blog.csdn.net/haorengoodman/article/details/23281343/ /** * 普通thread * 这是最常见的,创建一个thread ...
- 【转载】JAVA中线程的两种实现方法-实现Runnable接口和继承Thread类
转自: http://blog.csdn.net/sunguangran/article/details/6069317 非常感谢原作者,整理的这么详细. 在java中可有两种方式实现多线程,一种是继 ...
- java解析xml的4种经典方法
========================================== xml文件 <?xml version="1.0" encoding="GB2 ...
- java中this的N种使用方法
this可能是几乎所有有一点面向对象思想的语言都会引用到的变量,java自然不例外.只是,this有多少种用法,我也不知道了,让我们来see see. 由简入奢! 易. 来个例子说明下: public ...
随机推荐
- strace命令详解
转自: http://www.cnblogs.com/ahuo/p/4150623.html 备注: 这篇博文学到的不仅仅是 strace 这个命令,还有前辈的排错思路,致敬! strace 命令是一 ...
- 通过pyenv和virtualenv创建多版本Python虚拟环境
虚拟环境使用第三方工具virtualenv创建,首先输入以下命令检查系统是否已经安装virtualenv. $ virtualenv --version 如果显示virtualenv版本号,则说明已经 ...
- ts中interface与class的区别
interface -- 接口只声明成员方法,不做实现. class -- 类声明并实现方法. 那么接口有什么用呢?设想如下需求: 要实现一个print函数,它将传入的对象打印出来.在实际实现上,它将 ...
- 音频PCM编码
PCM编码 PCM就是把一个时间连续,取值连续的模拟信号变换成时间离散,取值离散的数字信号后在信道中传输.简而言之PCM就是对模拟信号先抽样,再对样值幅度量化,编码的过程.例如听到的声音就是模拟信号, ...
- python * 的区别
>>> ['Spam']*5 ['Spam', 'Spam', 'Spam', 'Spam', 'Spam'] >>> ['Spam'*5] ['SpamSpamS ...
- 关于mybatis-generator的问题
1.运行完mybatis-generator没有出现问题,但是代码看不到,出现这种东西: 你需要使用相对路径,如项目名+/src/main/java,就可以解决了 2.附录我的代码以供参考: < ...
- js和jquery通过this获取html标签中的属性值
<html> <head> <script type="text/javascript" src="jquery-1.10.2.min.js ...
- bootstrap-paginator分页插件的两种使用方式
分页有两种方式: 1. 前台分页:ajax一次请求获取全部数据,适合少量数据(万条数据以下): $.ajax({ type: "GET", url: "",// ...
- CTF---Web入门第六题 因缺思汀的绕过
因缺思汀的绕过分值:20 来源: pcat 难度:中 参与人数:6479人 Get Flag:2002人 答题人数:2197人 解题通过率:91% 访问解题链接去访问题目,可以进行答题.根据web题一 ...
- Codechef:Path Triples On Tree
Path Triples On Tree 题意是求树上都不相交或者都相交的路径三元组数量. 发现blog里没什么树形dp题,也没有cc题,所以来丢一道cc上的树形dp题. 比较暴力,比较恶心 #inc ...