55 java集合和泛型_5 _List实现类 _ LinkedList 、 ArrayList与LinkedList的区别
List实现类
- ArrayList【重点】:(内部实质也是数组)
- 数组结构实现,查询快、增刪慢;
- JDK1.2版本,运行效率快、线程不安全。
- LinkedList:链表结构实现,增删快,查询慢
- Vector:(基本不再使用)
- 数组结构实现,查询快、增删慢;
- JDK1. 0版本,运行效率慢、线程安全。
LinkedList
- 链表结构,增删快,查询慢
List
接口的链接列表实现。实现所有可选的列表操作,并且允许所有元素(包括null
)。除了实现List
接口外,LinkedList
类还为在列表的开头及结尾get
、remove
和insert
元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列 或 双端队列
代码:
package com.wlw.collection.list;
import com.wlw.collection.Student;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* LinkedList 的使用
* 存储结构:双向链表
*/
public class LinkedList_Demo {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
Student s1 = new Student("张三",20);
Student s2 = new Student("张无忌",21);
Student s3 = new Student("张大头",22);
//1.添加
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println("元素个数:"+linkedList.size());
System.out.println(linkedList.toString());
//2.删除
/*
linkedList.remove(0);
linkedList.remove(new Student("张三",20)); //也是可以把s1删除的,因为Student类中重写了equals方法
linkedList.remove(s2);
System.out.println("删除之后,元素个数:"+linkedList.size());
System.out.println("删除之后:"+linkedList.toString());
*/
//3.遍历【重点】
//3.1
System.out.println("---------------3.1 普通for循环---------------");
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
//3.2增强for循环
System.out.println("---------------3.2增强for循环---------------");
for (Object o : linkedList) {
Student s = (Student)o;
System.out.println(s.toString());
}
//3.3 Iterator迭代器
System.out.println("---------------3.3 Iterator迭代器---------------");
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()){
Student s = (Student)iterator.next();
System.out.println(s.toString());
}
//3.4 列表迭代器ListIterator,和Iterator的区别:ListIterator可以向前也可以向后遍历、添加、修改、删除元素
System.out.println("---------------3.4 列表迭代器ListIterator---------------");
ListIterator listIterator = linkedList.listIterator();
System.out.println("从前往后");
while(listIterator.hasNext()){
Student s = (Student) listIterator.next();
System.out.println(s.toString());
}
System.out.println("从后往前");
while (listIterator.hasPrevious()){
Student s = (Student) listIterator.previous();
System.out.println(s.toString());
}
//4.判断
System.out.println(linkedList.contains(s1)); //true
System.out.println(linkedList.isEmpty()); //false
//5.查找
System.out.println(linkedList.indexOf(s2)); //返回元素对应的下标
}
}
/*
元素个数:3
[Student{name='张三', age=20}, Student{name='张无忌', age=21}, Student{name='张大头', age=22}]
---------------3.1 普通for循环---------------
Student{name='张三', age=20}
Student{name='张无忌', age=21}
Student{name='张大头', age=22}
---------------3.2增强for循环---------------
Student{name='张三', age=20}
Student{name='张无忌', age=21}
Student{name='张大头', age=22}
---------------3.3 Iterator迭代器---------------
Student{name='张三', age=20}
Student{name='张无忌', age=21}
Student{name='张大头', age=22}
---------------3.4 列表迭代器ListIterator---------------
从前往后
Student{name='张三', age=20}
Student{name='张无忌', age=21}
Student{name='张大头', age=22}
从后往前
Student{name='张大头', age=22}
Student{name='张无忌', age=21}
Student{name='张三', age=20}
true
false
1
*/
LinkedList部分源码分析
transient int size = 0; //容量大小
transient Node<E> first;//链表的头节点
transient Node<E> last; //链表的尾节点
public LinkedList() { //无参构造
}
//add()
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
private static class Node<E> {//结点
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
ArrayList与LinkedList的区别
不同结构的实现方式:
-
ArrayList:必须开辟连续空间,查询快,增删慢。
-
LinkedList:无需开辟连续空间,查询慢,增删快。