Skip to content

Commit aa65bb6

Browse files
William FisetWilliam Fiset
authored andcommitted
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy