主要是作爲JAVA初學者對於常用數據結構的一些理解和總結
List
線性表
ArrayList
順序表(動態數組)–元素不限於int等
屬性
- elementData-儲存數據
- size-儲存個數
函數
- 構造函數-ArrayList()
- 無參數-(initial capacity 10)
- initial size(實現能夠判斷數組大小)
- 拷貝構造函數(???此處關於collection相關知識https://www.cnblogs.com/skywang12345/p/3308556.html) https://zhuanlan.zhihu.com/p/27873515
- add(int index,E element)
- remove(int index)
- get(index)
特性
- 非線程安全(???)
- 關於equal -https://zhuanlan.zhihu.com/p/27562748
LinkedList
鏈式表
(leetcode裏面題目中大多數使用的是單鏈表-單純val&next,而JAVA中的LinkedList 則指的是雙向鏈表,對於用法要進行一定的區分)
Set
集合
Map
Stack
Queue
隊列是一種特殊的線性表-FIFO
- 新建(LinkedList類是實現類,它實現了Queue接口)-
Queue<Object> queue = new LinkedList<>()
- 添加元素
queue.offer(element)
- 返回元素(同時刪除)
element = queue.poll()
- 返回元素(不刪除-空隊列返回
null
)element = queue.peek()
*判斷是否爲空queue.isEmpty()
import java.util.LinkedList;
public class Queue {
public static void main(String[] args)
{
LinkedList<String> queue = new LinkedList<String>(); // LinkedList是實現Queue接口的,所以LinkedList可以用Queue接口的方法
queue.offer("a"); //offer 爲入隊
queue.offer("b");
while(!queue.isEmpty())
{
System.out.println(queue.poll()); //poll 爲出隊
}
}
}