-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathStack.java
More file actions
32 lines (25 loc) · 754 Bytes
/
Stack.java
File metadata and controls
32 lines (25 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package StackArrayList;
import java.util.ArrayList;
import java.util.Collections;
/**
* Implement Stack<E> by adding the push, pop, and isEmpty functions. It must pass the prewritten unit tests.
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {
private ArrayList elements;
public Stack() {
this.elements = new ArrayList();
}
public E push(E element) {
this.elements.add(element);
return element;
}
public boolean isEmpty() {
return this.elements.isEmpty();
}
public E pop() throws IndexOutOfBoundsException {
Collections.reverse(this.elements);
E element = (E) this.elements.remove(0);
return element;
}
}