php网站培训班,前程无忧网站开发待遇怎么样,263企业邮箱入口登录找回密码,wordpress管理地址链表进阶 1. ArrayList的缺陷2. 链表2.1 链表的概念及结构2.2 链表的实现 3.链表面试题4.LinkedList的使用5.1 什么是LinkedList4.2 LinkedList的使用 5. ArrayList和LinkedList的区别 1. ArrayList的缺陷 
通过源码知道#xff0c;ArrayList底层使用数组来存储元素#xff1…   链表进阶 1. ArrayList的缺陷2. 链表2.1 链表的概念及结构2.2 链表的实现 3.链表面试题4.LinkedList的使用5.1 什么是LinkedList4.2 LinkedList的使用 5. ArrayList和LinkedList的区别  1. ArrayList的缺陷 
通过源码知道ArrayList底层使用数组来存储元素 
public class ArrayListE extends AbstractListE
implements ListE, RandomAccess, Cloneable, java.io.Serializable
{
// ...
// 默认容量是10
private static final int DEFAULT_CAPACITY  10;
//...
// 数组用来存储元素
transient Object[] elementData; // non-private to simplify nested class access
// 有效元素个数
private int size;
public ArrayList(int initialCapacity) {
if (initialCapacity  0) {
this.elementData  new Object[initialCapacity];
} else if (initialCapacity  0) {
this.elementData  EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException(Illegal Capacity: 
initialCapacity);
}
}
}
// ...由于其底层是一段连续空间**当在ArrayList任意位置插入或者删除元素时就需要将后序元素整体往前或者往后搬移时间复杂度为O(n)**效率比较低因此ArrayList不适合做任意位置插入和删除比较多的场景。因此java集合中又引入了LinkedList即链表结构。 
2. 链表 
2.1 链表的概念及结构 
链表是一种物理存储结构上非连续存储结构数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。 链表的结构就类似于火车  实际中链表的结构非常多样以下情况组合起来就有8种链表结构 1. 单向或者双向  2. 带头或者不带头  3. 循环或者非循环  虽然有这么多的链表的结构但是我们重点掌握两种 无头单向非循环链表结构简单一般不会单独用来存数据。**实际中更多是作为其他数据结构的子结构如哈希桶、图的邻接表等等。**另外这种结构在笔试面试中出现很多  无头双向链表在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。 
2.2 链表的实现 
定义一个接口 
public interface IList {//头插法public void addFirst(int data);//尾插法public void addLast(int data);//任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data);//查找是否包含关键字key是否在单链表当中public boolean contains(int key);//删除第一次出现关键字为key的节点public void remove(int key);//删除所有值为key的节点public void removeAllKey(int key);//得到单链表的长度public int size();//清空单链表public void clear();//遍历单链表public void display();//创建单链表public void  crate();
}定义一个MySIngList类 准备工作MySingeList要继承IList接口的所有方法 
public class MySingeList implements IList {
//定义单链表static class ListNode {public int val;public ListNode next;//next存的是下一个节点的地址/*** 就相当于Person personnew person()next是一个引用* next存的是地址next引用类型是ListNode类型的引用*/public ListNode(int val) {//实例化this.val  val;}}
//定义一个头结点public ListNode head; 
创建一个单链表 Overridepublic void crate() {ListNode listNode1  new ListNode(11);//如何修改当前节点位置的next的值为指定节点位置ListNode listNode5  new ListNode(22);ListNode listNode4  new ListNode(33);ListNode listNode3  new ListNode(44);ListNode listNode2  new ListNode(55);listNode1.next  listNode2;listNode2.next  listNode3;listNode3.next  listNode4;listNode4.next  listNode5;this.head  listNode1;//将插入的第一个节点定义为头节点} 
遍历这个单链表 Overridepublic void display() {//定义一个cur让cur去走head不变才能在遍历以后找到headListNode car  head.next;while (car ! null) {//判断是否遍历完链表System.out.println(car.val   );car  car.next;//如何从当前位置走到下一个节点位置}}头插法 //头插法;时间复杂度O(1)Overridepublic void addFirst(int data) {ListNode listNode  new ListNode(data);if (this.head  null) {this.head  listNode;} else {this.head  listNode.next;this.head  listNode;}}不同位置添加的时间复杂度不同常见的错误观点是认为所有添加操作都是O(1)。在尾部添加需要O(n)头部添加为O(1)在任意位置则平均为O(n)。了解这些有助于优化链表操作的效率。 尾插法 //尾插法时间复杂度O(n)Overridepublic void addLast(int data) {ListNode car  this.head;ListNode listNode  new ListNode(data);if (this.head  null) {this.head  listNode;} else {//找到最后一个节点while (car.next ! null) {car  car.next;}//car现在指向最后一个节点/**如果想让car停在最后一个节点的位置 cur.nextnull* 如果想把整个链表的每一个节点都遍历完那么就是carnull* */car.next  listNode;}}在链表尾部添加addLast()需要从头遍历时间复杂度为O(n) 
任意位置插入,第一个数据节点为0号下标 
//随便插入
private ListNode seach(int index) {ListNode car  this.head;int count  0;while (count ! index - 1) {car  car.next;count;}return car;}erridepublic void addIndex(int index, int data) {if (index  0 || index  size()) {System.out.println(不合法);throw new Poslllgality(插入元素下标异常  data);}if (index  0) {addFirst(data);return;}if (index  size()) {addLast(data);return;}ListNode car  seach(index);ListNode node  new ListNode(data);node.next  car.next;car.next  node;}当在任意位置插入的时候要考虑的情况有很多 
当index  0 || index  size()的时候抛出一个异常当index为0的时候头插法当index为size的时候尾插法正常的插入法 
查找是否包含关键字key是否在单链表当中 Overridepublic boolean contains(int key) {ListNode car  this.head;while (car ! null) {if (car.val  key) {return false;}car  car.next;}return false;}删除第一次出现关键字为key的节点 
Overridepublic void remove(int key) {if (this.head  null) {System.out.println(无法删除);}if (this.head.val  key) {this.head  null;//或者this.headthis.head.next}ListNode car  searchprev(key);if (car  null) {System.out.println(没有要删除的数字);return;} else {ListNode del  car.next;//通过car.next找到del的位置car.next  del.next;//然后就可以自己看懂}}//写一个方法,找到关键字看的前一个节点的地址private ListNode searchprev(int key) {ListNode car  this.head;while (car.next ! null) {if (car.next.val  key) {return car;}car  car.next;}return null;}删除所有值为key的节点 Overridepublic void removeAllKey(int key) {if (this.head  null) {return;}ListNode prev  head;ListNode car  head.next;while (car ! null) {if (car.val  key) {prev.next  car.next;car  car.next;} else {prev  car;car  car.next;}}if (head.val  key) {head  head.next;}}得到单链表的长度 Overridepublic int size() {ListNode car  head.next;int count  0;while (car ! null) {count;car  car.next;}return count;}清空链表 Overridepublic void clear() {ListNode car  this.head;while (car ! null) {ListNode carNext  car.next;//定义一个变量把car的next记录下/* car.valnull;如果是一个应用数据类型那么才写这个如果不是car.nextnull就可以清除完毕*/car.next  null;car  carNext;}head  null;//上面的循环走完但是head还没有置空要手动把head置空}3.链表面试题 
删除链表中等于给定值 val 的所有节点。 力扣反转一个单链表。 力扣给定一个带有头结点 head 的非空单链表返回链表的中间结点。如果有两个中间结点则返回第二个中间结点。力扣输入一个链表输出该链表中倒数第k个结点。 牛客将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。力扣编写代码以给定值x为基准将链表分割成两部分所有小于x的结点排在大于或等于x的结点之前 。力扣 
4.LinkedList的使用 
5.1 什么是LinkedList LinkedList的底层是双向链表结构(链表后面介绍)由于链表没有将元素存储在连续的空间中元素存储在单独的节 点中然后通过引用将节点连接起来了因此在在任意位置插入或者删除元素时不需要搬移元素效率比较高。  在集合框架中LinkedList也实现了List接口具体如下  【说明】 
LinkedList实现了List接口LinkedList的底层使用了双向链表LinkedList没有实现RandomAccess接口因此LinkedList不支持随机访问LinkedList的任意位置插入和删除元素时效率比较高时间复杂度为O(1)LinkedList比较适合任意位置插入的场景 
4.2 LinkedList的使用 
LinkedList的构造 public static void main(String[] args) {
// 构造一个空的LinkedList
ListInteger list1  new LinkedList();
ListString list2  new java.util.ArrayList();
list2.add(JavaSE);
list2.add(JavaWeb);
list2.add(JavaEE);
// 使用ArrayList构造LinkedList
ListString list3  new LinkedList(list2);
}LinkedList的其他常用方法介绍  
public static void main(String[] args) {
LinkedListInteger list  new LinkedList();
list.add(1); // add(elem): 表示尾插
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println(list.size());
System.out.println(list);
// 在起始位置插入0
list.add(0, 0); // add(index, elem): 在index位置插入元素elem
System.out.println(list);
list.remove(); // remove(): 删除第一个元素内部调用的是removeFirst()
list.removeFirst(); // removeFirst(): 删除第一个元素
list.removeLast(); // removeLast(): 删除最后元素
list.remove(1); // remove(index): 删除index位置的元素
System.out.println(list);
// contains(elem): 检测elem元素是否存在如果存在返回true否则返回false
if(!list.contains(1)){
list.add(0, 1);
list.add(1);
System.out.println(list);
System.out.println(list.indexOf(1)); // indexOf(elem): 从前往后找到第一个elem的位置
System.out.println(list.lastIndexOf(1)); // lastIndexOf(elem): 从后往前找第一个1的位置
int elem  list.get(0); // get(index): 获取指定位置元素
list.set(0, 100); // set(index, elem): 将index位置的元素设置为elem
System.out.println(list);
// subList(from, to): 用list中[from, to)之间的元素构造一个新的LinkedList返回
ListInteger copy  list.subList(0, 3);
System.out.println(list);
System.out.println(copy);
list.clear(); // 将list中元素清空
System.out.println(list.size());
}
}LinkedList的遍历 
public static void main(String[] args) {
LinkedListInteger list  new LinkedList();
list.add(1); // add(elem): 表示尾插
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println(list.size());
// foreach遍历
for (int e:list) {
System.out.print(e   );
}
System.out.println();
// 使用迭代器遍历---正向遍历
ListIteratorInteger it  list.listIterator();
while(it.hasNext()){
System.out.print(it.next()  );
}
System.out.println();
// 使用反向迭代器---反向遍历
ListIteratorInteger rit  list.listIterator(list.size());
while (rit.hasPrevious()){
System.out.print(rit.previous()  );
}
System.out.println();
}5. ArrayList和LinkedList的区别 
 文章转载自: http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.skcmt.cn.gov.cn.skcmt.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.qwgct.cn.gov.cn.qwgct.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn http://www.morning.lwxsy.cn.gov.cn.lwxsy.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.prysb.cn.gov.cn.prysb.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.yqgbw.cn.gov.cn.yqgbw.cn http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.lhxkl.cn.gov.cn.lhxkl.cn http://www.morning.mgbcf.cn.gov.cn.mgbcf.cn http://www.morning.yhxhq.cn.gov.cn.yhxhq.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn