栈
- 栈就是一个先进后出的结构,可以用数组来组成也能用链表来组成,只是每次新增和删除都只能从栈顶取
public class MyStack<E> {
public MyStack() {
}
/**
* 压数入栈
*
* @param item 入栈的参数
* @return
*/
public E push(E item) {
// 里面需要先判断这个入栈之后会不会超过数组最大数,然后超过就扩容,没超过就直接赋值
addElement(item);
return item;
}
/**
* 出栈 然后删除出栈的元素
* @return
*/
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
/**
* 出栈但是不删除出山的元素,先判断是否栈的元素为0,为0栈里没有数据,就没有出栈的必要了,直接抛异常即可
* @return
*/
public synchronized E peek() {
int len = size();
if (len == 0) {
throw new EmptyStackException();
}
return elementAt(len - 1);
}
/**
* 判断栈内是否有元素
* @return
*/
public boolean empty() {
return size() == 0;
}
/**
* 判断栈内是否有这个元素,先判断是否在查询的时候有数据新增,有的话报错
* 然后判断是否为null,为null的话有==判断,不为null的话用equals判断
* @param o
* @return
*/
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
// 以下可以和list共用
// =======================================================
protected Object[] elementData;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
protected int elementCount;
protected transient int modCount = 0;
protected int capacityIncrement;
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0) {
grow(minCapacity);
}
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
if (newCapacity - MAX_ARRAY_SIZE > 0) {
newCapacity = hugeCapacity(minCapacity);
}
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
public synchronized int size() {
return elementCount;
}
public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
} else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount - 1);
}
public synchronized int lastIndexOf(Object o, int index) {
if (index >= elementCount) {
throw new IndexOutOfBoundsException(index + " >= " + elementCount);
}
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 E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
return elementData(index);
}
E elementData(int index) {
return (E) elementData[index];
}
}