-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathGenericStack.java
More file actions
41 lines (29 loc) · 955 Bytes
/
GenericStack.java
File metadata and controls
41 lines (29 loc) · 955 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
package StackArray;
import java.util.Arrays;
import java.util.Stack;
/**
* Expand the ArrayList implementation of stack here to use an E[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
* @param <E>
*/
public class GenericStack<E> {
private E[] elements;
// GenericStack<String> myStack = new GenericStack<>();
public GenericStack() {
elements = (E[]) new Object[0];
//casting to just an object: (not array)
// element = (E) new Object;
}
public boolean isEmpty() {
return elements.length==0;
}
public void push(E foobar) {
elements= Arrays.copyOf(elements, elements.length+1);
elements[elements.length-1] = foobar;
}
public E pop() {
E lastElement = (E) elements[elements.length-1];
elements=Arrays.copyOf(elements,elements.length-1);
return lastElement;
}
}