Skip to content

Commit aa65bb6

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
Updated ArrayStack
1 parent da8d93a commit aa65bb6

File tree

1 file changed

+5
-9
lines changed
  • src/main/java/com/williamfiset/algorithms/datastructures/stack

1 file changed

+5
-9
lines changed

src/main/java/com/williamfiset/algorithms/datastructures/stack/ArrayStack.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
/** @author liujingkun */
77
public class ArrayStack<T> implements Stack<T> {
8+
private int size;
89
private int capacity;
910
private Object[] data;
10-
private int size;
1111

1212
public ArrayStack() {
1313
capacity = 16;
14-
size = 0;
1514
data = new Object[capacity];
1615
}
1716

@@ -28,17 +27,14 @@ public boolean isEmpty() {
2827
@Override
2928
public void push(T elem) {
3029
if (size == capacity) {
31-
adjustCap();
30+
increaseCapacity();
3231
}
3332
data[size++] = elem;
3433
}
3534

36-
// adjust the capacity to store more element
37-
private void adjustCap() {
38-
if (capacity == Integer.MAX_VALUE) {
39-
throw new OutOfMemoryError();
40-
}
41-
capacity += capacity;
35+
// Increase the capacity to store more elements.
36+
private void increaseCapacity() {
37+
capacity *= 2;
4238
data = Arrays.copyOf(data, capacity);
4339
}
4440

0 commit comments

Comments
 (0)