Stack<Integer> st = new Stack<>();
泛型类型必须被指定为引用数据类型。
| API | 功能概述 | 备注 |
|---|---|---|
| void push(T value) | 插入新的元素 | 支持向上转型,因此value可以是基本数据类型 |
| T pop( ) | 弹出栈顶元素 | |
| T peek( ) | 查看栈顶元素(不移除) | 不会移除该元素 |
| boolean isEmpty( ) | 判断栈是否为空 | |
| int size( ) | 返回栈的元素个数 | |
| T[] toArray(T[] arrObj) | 将栈转换成数组 | 传入的参数是一个引用数据类型的数组对象 举例:Integer[] arr = st.toArray(new Integer[0]) |
public static void testStack(){
// 泛型必须指定为引用数据类型
Stack<Integer> st = new Stack<>();
// 01.入栈
st.push(1);
st.push(2);
st.push(3);
// 02.出栈
int popResult = st.pop();
System.out.println(popResult); // 3
// 03.查看栈顶结果
int peekResult = st.peek();
System.out.println(peekResult); // 2
// 04.判断是否为空
boolean isEmpty = st.isEmpty();
System.out.println(isEmpty); // false
// 05.获取元素个数
int size = st.size();
System.out.println(size); // 2
// 06.转成数组 - 类型必须是引用数据类型
Integer[] stToArr = st.toArray(new Integer[0]);
System.out.println(Arrays.toString(stToArr)); // [1, 2]
}
Queue<Integer> queue = new LinkedList<>();
使用 LinkedList 实现即可,这也几乎是LinkedList最常见的用途了。
| API | 功能概述 | 备注 |
|---|---|---|
| boolean offer(T value) | 插入新的元素 | 支持向上转型,因此value可以是基本数据类型 若队列已满,返回false |
| T poll( ) | 队首元素出队 | 若队列为空,返回null |
| T peek( ) | 查看队首元素(不移除) | 不会移除该元素 若队列为空,返回null |
| boolean isEmpty( ) | 判断队列是否为空 | |
| int size( ) | 返回队列的元素个数 | |
| T[] toArray(T[] arrObj) | 将队列转换成数组 | 传入的参数是一个引用数据类型的数组对象 举例:Integer[] arr = queue.toArray(new Integer[0]) |
public static void testQueue(){
Queue<Integer> queue = new LinkedList<>();
// 01.入队
queue.offer(1);
queue.offer(2);
queue.offer(3);
// 02.出队
int pollResult = queue.poll();
System.out.println(pollResult); // 1
// 03.查看队首结果
int peekResult = queue.peek();
System.out.println(peekResult); // 2
// 04.判断队列是否为空
boolean isEmpty = queue.isEmpty(); // false
System.out.println(isEmpty);
// 05.获取元素个数
int size = queue.size(); // 2
System.out.println(size);
// 06.转成数组
Integer[] queueToArr = queue.toArray(new Integer[0]);
System.out.println(Arrays.toString(queueToArr)); // [2, 3]
}
Deque<Integer> dq = new LinkedList<>();
双端队列同样使用 LinkedList。
双端队列中,offer(T value)、poll( )、peek( )方法依旧是可用的
但既然是双端队列,建议还是按照其特性使用与之等价的 offerLast(T value)、pollFirst( ) 和 peekFirst( )。
| API | 功能概述 | 备注 |
|---|---|---|
| boolean offerFirst(T value) | 队首插入新的元素 | 支持向上转型,因此value可以是基本数据类型 若队列已满,返回false |
| boolean offerLast(T value) | 队尾插入新的元素,与offer(T value)等价 | 支持向上转型,因此value可以是基本数据类型 若队列已满,返回false |
| T pollFirst( ) | 队首元素出队,与poll( )等价 | 若队列为空,返回null |
| T pollLast( ) | 队首元素出队 | 若队列为空,返回null |
| T peekFirst( ) | 查看队首元素(不移除),与peek( )等价 | 不会移除该元素 若队列为空,返回null |
| T peekLast( ) | 查看队首元素(不移除) | 不会移除该元素 若队列为空,返回null |
| boolean isEmpty( ) | 判断队列是否为空 | |
| int size( ) | 返回队列的元素个数 | |
| T[] toArray(T[] arrObj) | 将队列转换成数组 | 传入的参数是一个引用数据类型的数组对象 举例:Integer[] arr = dq.toArray(new Integer[0]) |
public static void testDeque() {
Deque<Integer> dq = new LinkedList<>();
// 01.队头入队
dq.offerFirst(3);
dq.offerFirst(2);
dq.offerFirst(1);
// 02.队尾入队 - offer(T value) 与 offerLast(T value)等价
dq.offer(4);
dq.offerLast(5);
// 03.队头出队 - poll( ) 与 pollLast( )等价
int pollResult = dq.poll();
int pollFirstResult = dq.pollFirst();
System.out.println(pollResult); // 1
System.out.println(pollFirstResult); // 2
// 04.队尾出队
int pollLastResult = dq.pollLast();
System.out.println(pollLastResult); // 5
// 05.查看队首结果 - peek( ) 与 peekFirst( )等价
int peekResult = dq.peek();
int peekFirstResult = dq.peekFirst();
System.out.println(peekResult); // 3
System.out.println(peekFirstResult); // 3
// 06.查看队尾结果
int peekLastResult = dq.peekLast(); // 4
System.out.println(peekLastResult);
// 07.判断队列是否为空
boolean isEmpty = dq.isEmpty(); // false
System.out.println(isEmpty);
// 08.获取元素个数
int size = dq.size(); // 2
System.out.println(size);
// 09.转成数组
Integer[] queueToArr = dq.toArray(new Integer[0]);
System.out.println(Arrays.toString(queueToArr)); // [3, 4]
}