-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathObjectStack.java
More file actions
99 lines (79 loc) · 2.23 KB
/
ObjectStack.java
File metadata and controls
99 lines (79 loc) · 2.23 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package StackArray;
import java.util.Arrays;
/**
* Expand the ArrayList implementation of stack here to use an Object[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
*
* @param <E>
*/
public class ObjectStack<E> {
private int nullValues;
private int size;
private static final int DEFAULT_SIZE = 10;
private Object[] elements;
public ObjectStack() {
elements = new Object[DEFAULT_SIZE];
size = 0;
nullValues = 0;
}
public void push(E object) {
add(object);
}
public boolean isEmpty() {
return size == 0;
}
public Object pop() throws IndexOutOfBoundsException {
if (this.isEmpty()) {
throw new IndexOutOfBoundsException("this list is empty");
}
return remove();
}
public boolean add(Object object) {
ensureCapasity(size + 1);
this.elements[size++] = object;
//size++;
return true;
}
public int indexOf(Object theObject) {
for (int i = 0; i < size; i++) {
if (theObject.equals(elements[i])) {
return i;
}
}
return -1;
}
public void shift(int index) {
Object theObject = null;
for (int i = index; i < size; i++) {
theObject = elements[i];
if (elements[i + 1] != null)
elements[i + 1] = theObject;
}
}
public void ensureCapasity(int size) {
if (size - elements.length > 0) {
grow();
}
}
public void grow() {
int newCapasity = elements.length + DEFAULT_SIZE;
Arrays.copyOf(elements, newCapasity);
}
public Object remove() {
Object removedObject = elements[size - 1];
elements[size - 1] = null;
//numberOfNulls++;
//checkToResize(numberOfNulls);
size--;
return removedObject;
}
public void checkToResize(int theNumber) {
int length = elements.length - DEFAULT_SIZE;
if (length > 10 && theNumber == 10) {
resize(length);
}
}
public void resize(int minLength) {
elements = Arrays.copyOf(elements, minLength);
}
}