一、java中栈与其他集合的关系
1.Stack与其他相关数据结构接口的关系
2.Stack与Vector
- Vector是Stack的父类,它实现了Stack实现与之相关接口的所有方法
3.Vector与ArrayList
- 相同点:
- 它们都实现了List接口和Collection接口等
- 它们的底层都使用数组来存储节点
- 不同点
- (1)线程安全:ArrayList是不是线程安全的,而Vector是线程安全的(多线程操作)
- (2)扩容:ArrayList容量增长率为当前容量的50%,而Vector是100%,对于数据量比较大的数据,Vector减少了扩容开销
- (3)性能:因为ArrayList不是线程安全的,没有使用synchoronized线程锁,所以性能更高,而Vector使用了synchoronized线程锁,所以速度没有ArrayList快
二、Stack的使用与实现原理解读
因为Stack实现了List接口,而且底层也使用数组存储数据节点,因此与ArrayList很类似,在此不再赘述
1.初始化
- new Stack() : 不需要任何参数,返回一个Stack对象
Stack<String> books = new Stack<String>();
2.push方法
(1)push方法的使用
- push(E element) : 向栈中添加节点元素
Stack<String> books = new Stack<String>();
books.push("JAVA编程");
books.push("网络安全");
books.push("计算机网络");
books.push("计算机文化基础");
System.out.println(books); //[JAVA编程, 网络安全, 计算机网络, 计算机文化基础]
(2)push方法的实现
public E push(E item) {
/*-------------------------------------------------
public synchronized void addElement(E obj) {
modCount++;
//扩容
ensureCapacityHelper(elementCount + 1);
//将元素添加到数组末尾,并增加元数个数
elementData[elementCount++] = obj;
}
---------------------------------------------------*/
addElement(item);
return item;
}
3.pop方法
(1)pop方法的使用
- pop:出栈操作
Stack<String> books = new Stack<String>();
books.push("JAVA编程");
books.push("网络安全");
books.push("计算机网络");
books.push("计算机文化基础");
String lastBookName = books.pop();
System.out.println(lastBookName); //计算机文化基础
System.out.println(books); //[JAVA编程, 网络安全, 计算机网络]
(2)pop方法的实现解读
public synchronized E pop() {
E obj;
int len = size();
//1.通过调用peek方法获取最后一个节点
obj = peek();
/*-------------------------------------------------
public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
//1.获取需要移动的元素的长度
int j = elementCount - index - 1;
if (j > 0) {
//2.通过复制数组的方式将数组往前移动
System.arraycopy(elementData, index + 1, elementData, index, j);
}
//3.元素节点个数减1
elementCount--;
//4.将最后一位元素设置为null
elementData[elementCount] = null; /* to let gc do its work */
}
--------------------------------------------------*/
//2.调用removeElementAt方法删除最后一个节点
removeElementAt(len - 1);
return obj;
}
4.peek方法
(1)peek方法的使用
- peek方法:返回栈的最后一个元素节点
(2)peek方法的实现解读
-
public synchronized E peek() {
//1.获取数组的长度
int len = size();
if (len == 0)
throw new EmptyStackException();
/*-----------------------------------------------------
public synchronized E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
/*---------------------------------------------------
E elementData(int index) {
//通过索引值从数组中查找元素
return (E) elementData[index];
}
*----------------------------------------------------/
return elementData(index);
}
-------------------------------------------------------*/
return elementAt(len - 1);
}
5.empty方法
(1)empty方法的使用
- empty判断栈是否为空栈
Stack<String> books = new Stack<String>();
System.out.println(books.empty()); //true
books.push("大学英语");
System.out.println(books.empty()); //false
(2)empty方法的实现解读
/*------------------------------------------------
public synchronized int size() {
return elementCount;
}
--------------------------------------------------*/
public boolean empty() {
//判断元素个数是否为0即可
return size() == 0;
}
6.search方法
(1)search方法的使用
- search(E element):搜索元素在栈中的出现的位置(不存在时,返回-1)
Stack<String> books = new Stack<String>();
books.push("JAVA编程");
books.push("网络安全");
books.push("计算机网络");
books.push("计算机文化基础");
int idx = books.search("计算机网络");
System.out.println(idx); //2
(2)search方法的实现解读
public synchronized int search(Object o) {
/*----------------------------
public synchronized int lastIndexOf(Object o, int index) {
if (index >= elementCount)
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
//如果元素为null
if (o == null) {
//从最后一个元素开始查找,遍历栈的节点
for (int i = index; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
//从最后一个元素开始查找,遍历栈的节点
for (int i = index; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}
-----------------------------*/
//1.查找出元素在栈中最后一次出现的位置
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}