forked from Beerkay/JavaMultiThreading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomStack.java
More file actions
34 lines (26 loc) · 771 Bytes
/
CustomStack.java
File metadata and controls
34 lines (26 loc) · 771 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
package Generics;
public class CustomStack<E> {
private E[] elements;
private int size = 0;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
public CustomStack() {
// elements = (E[]) new Object[][DEFAULT_INITIAL_CAPACITY];
}
public void push(E e){
ensureCapacity();
elements[size++] = e;
}
public void pop(){
if (size == 0){
throw new IllegalStateException("Stack is empty");
}
elements[--size] = null;
}
private void ensureCapacity(){
if (elements.length == size){
Object[] newElements = new Object[2 * size + 1];
System.arraycopy(elements, 0, newElements, 0, size);
// elements = newElements;
}
}
}