No effective change - ArrayStack cleanup

This commit is contained in:
Robin Bygrave
2015-08-01 22:00:00 +12:00
parent 0d2615522f
commit da65256208
2 changed files with 170 additions and 67 deletions
@@ -10,80 +10,80 @@ import java.util.EmptyStackException;
*/
public class ArrayStack<E> {
private final ArrayList<E> list;
private final ArrayList<E> list;
/**
* Creates an empty Stack with an initial size.
*/
public ArrayStack(int size) {
this.list = new ArrayList<E>(size);
}
/**
* Creates an empty Stack with an initial size.
*/
public ArrayStack(int size) {
this.list = new ArrayList<E>(size);
}
/**
* Creates an empty Stack.
*/
public ArrayStack() {
this.list = new ArrayList<E>();
}
/**
* Creates an empty Stack.
*/
public ArrayStack() {
this.list = new ArrayList<E>();
}
/**
* Pushes an item onto the top of this stack.
*/
public E push(E item) {
list.add(item);
return item;
}
/**
* Pushes an item onto the top of this stack.
*/
public void push(E item) {
list.add(item);
}
/**
* Removes the object at the top of this stack and returns that object as
* the value of this function.
*/
public E pop() {
int len = list.size();
E obj = peek();
list.remove(len - 1);
return obj;
/**
* Removes the object at the top of this stack and returns that object as
* the value of this function.
*/
public E pop() {
int len = list.size();
if (len == 0) {
throw new EmptyStackException();
}
return list.remove(len - 1);
}
protected E peekZero(boolean retNull) {
int len = list.size();
if (len == 0) {
if (retNull) {
return null;
}
throw new EmptyStackException();
}
return list.get(len - 1);
protected E peekZero(boolean retNull) {
int len = list.size();
if (len == 0) {
if (retNull) {
return null;
}
throw new EmptyStackException();
}
return list.get(len - 1);
}
/**
* Returns the object at the top of this stack without removing it.
*/
public E peek() {
return peekZero(false);
}
/**
* Returns the object at the top of this stack without removing it.
* If the stack is empty this returns null.
*/
public E peekWithNull() {
return peekZero(true);
}
/**
* Tests if this stack is empty.
*/
public boolean isEmpty() {
return list.isEmpty();
}
/**
* Returns the object at the top of this stack without removing it.
*/
public E peek() {
return peekZero(false);
}
public int size(){
return list.size();
}
public boolean contains(Object o){
//noinspection SuspiciousMethodCalls
return list.contains(o);
}
/**
* Returns the object at the top of this stack without removing it.
* If the stack is empty this returns null.
*/
public E peekWithNull() {
return peekZero(true);
}
/**
* Tests if this stack is empty.
*/
public boolean isEmpty() {
return list.isEmpty();
}
public int size() {
return list.size();
}
public boolean contains(Object o) {
//noinspection SuspiciousMethodCalls
return list.contains(o);
}
}