-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStackArray.java
More file actions
42 lines (31 loc) · 764 Bytes
/
Copy pathStackArray.java
File metadata and controls
42 lines (31 loc) · 764 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
33
34
35
36
37
38
39
40
41
42
import java.lang.*;
import java.util.ArrayList;
public class StackArray<T> implements Coll<T>{
private ArrayList<T> data;
public StackArray() {
data = new ArrayList<T>();
}
public void add(T d) {
data.add(d);
}
public T remove() {
return(data.remove(data.size() - 1));
}
public boolean isEmpty() {
return(data.size() == 0);
}
public static void main(String[] args) {
StackArray<Integer> ll = new StackArray<Integer>();
for (int i = 0; i < 10; i++) {
ll.add(i * 2);
}
try {
while (!ll.isEmpty()) {
System.out.println(ll.remove());
}
} catch (Exception e) {
System.out.println("Attempted to remove from empty stack");
System.exit(-1);
}
}
}