List介绍
List是一个接口,继承了Collection。
List的两个最重要的子类分别是LinkedList和ArrayList。
ArrayList
基本概念
ArrayList的底层是一个长度可以动态增长的object数组,并且实现了三个没有方法的接口,RandomAccess、Cloneable、java.io.Serializable。
在JDK中默认的构造函数容量是0,
ArrayList常用方法及其源码
- add方法。
源码:
底层是一个可以动态增长的数组,默认容量是0,所以第一次调用add时,就要进行扩容。扩容源码
底层容量进行扩容,每次扩容都进行>>2,即扩容百分之50;
如果不够,那就直接把最小要求容量进行赋值。
同时size的值也要进行更新。
把元素赋给elementData(size)=e,size++。
使用:
**********!list默认不指定泛型时,存进去的对象都是object
ArrayList list=new ArrayList();
list.add(67);//向末尾增加元素 但是这里的67不是基本类型!!!
list.add(2,50);//向下表为2的地址增加元素
list.add(new Integet(50));
ArrayList list1=new ArrayList();
list1.add(45);
list1.add(54);
list1.add(45);
list.addAll(list1);//将list1的元素全部加到list的末尾。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- get方法
源码:
先进性范围判断。(E 指的是泛型,在创建类的时候设置的)
然后返回
使用:
Object obj=list.get(2);
int elem=(int) list.get(2);
- 1
- 2
-
Iterator-----迭代器
Iterator是一个接口,new了一个Itr对象。详细使用请见https://editor.csdn.net/md/?articleId=116155312 有详细描述。 -
set();------修改方法
list.set(1,52);
- 1
- remove();----删除
list.clean();-----全部清空
list.remove(new Integer(89));------按内容删
list.remove(2);--------按索引删
ArrayList的遍历
一共有三种方法。
- 使用for循环遍历
for(int i=0;i<list.size;i++){
int elem=list.get(i);
System.out.println(elem);
}
- 1
- 2
- 3
- 4
- 使用foreach循环
for(Object elem:list){
System.out.println(elem);
}
- 1
- 2
- 3
- 使用迭代器遍历
Iterator it=list.iterator();
while(it.hasNext()){
int elem=(int) it.next();
System.out.println(elem);
}
- 1
- 2
- 3
- 4
- 5
LinkedList
底层存储结构发生变化
LinkedList的底层存储结构不再是数组,而是双向链表。
功能底层实现变了
LinkedList的增加删除操作效率比ArrayList效率高的高。只用修改前后的指针就可,而ArrayList需要大量移动元素。但是ArrayList按照索引查找效率比Linked List高,ArrayList直接计算即可,而Linked List需要一个个遍历下来数数。
到底是使用ArrayList还是LinkedList
如果按照索引查询多的,则采用ArrayList;若删除添加操作多的则采用LinkedList。
LinkedList相比ArrayList提供了更多的方法
public class LinkedList Deque-----双管队列 可以当作栈也可以当作队列
implements List, Deque, Cloneable, java.io.Serializable 实现了多个接口
推荐向上转型,提高代码利用率
List list=new ArrayList();
linkedList源码
因为实现Deque,所以除了可以作为线性表,还可以当作队列和栈使用。
当用栈的时候,
//栈 //public class Stack<E> extends Vector<E> 已经过时 Stack stack=new Stack(); //双端队列 :可以作为栈来使用push()入栈 pop()出栈 peek()获取栈顶元素 Deque deque;//实现类 ArrayDeque 底层是数组 LinkedList 底层是链表
<span class="token comment">//模拟摞盘子---栈</span> Deque<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> deque1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> deque1<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">"盘子1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> deque1<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">"盘子2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>deque1<span class="token punctuation">.</span><span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> deque1<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">"盘子3"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>deque1<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> String s<span class="token operator">=</span>deque1<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
结果
用队列的时
//队列 Queue queue;//实现类 ArrayDeque 底层是数组 LinkedList 底层是链表
<span class="token comment">//模拟摞盘子---队列</span> queue<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queue<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span><span class="token string">"盘子5"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queue<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span><span class="token string">"盘子4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>queue<span class="token punctuation">.</span><span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queue<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span><span class="token string">"盘子7"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>queue<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> String s<span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> queue<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13