forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericStack2.java
More file actions
executable file
·86 lines (84 loc) · 2.16 KB
/
GenericStack2.java
File metadata and controls
executable file
·86 lines (84 loc) · 2.16 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
/* */ package ch19;
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ class GenericStack2<E>
/* */ {
/* 30 */ int capacity = 10;
/* 31 */ int count = 0;
/* 32 */ private E[] list = (E[])new Object[this.capacity];
/* */
/* */ public int getSize() {
/* 35 */ return this.count;
/* */ }
/* */
/* */ public E peek() throws Exception {
/* 39 */ if (isEmpty()) {
/* 40 */ throw new Exception("stack empty");
/* */ }
/* 42 */ return this.list[this.count - 1];
/* */ }
/* */
/* */ public void push(E o) {
/* 46 */ if (this.count < this.capacity) {
/* 47 */ this.list[this.count++] = o;
/* */ } else {
/* 49 */ System.out.println("capacity changed...");
/* 50 */ System.out.println("before: capacity = " + this.capacity);
/* 51 */ System.out.println("before: count = " + this.count);
/* 52 */ E[] temp = (E[])new Object[this.capacity * 2];
/* */
/* 54 */ System.arraycopy(this.list, 0, temp, 0, this.capacity);
/* 55 */ this.list = temp;
/* 56 */ this.list[this.count++] = o;
/* 57 */ this.capacity *= 2;
/* 58 */ System.out.println("after: capacity = " + this.capacity);
/* 59 */ System.out.println("after: count = " + this.count);
/* */ }
/* */ }
/* */
/* */ public E pop() throws Exception {
/* 64 */ if (isEmpty()) {
/* 65 */ throw new Exception("stack empty");
/* */ }
/* 67 */ E o = this.list[this.count - 1];
/* 68 */ this.count--;
/* 69 */ return o;
/* */ }
/* */
/* */ public boolean isEmpty() {
/* 73 */ return (this.count == 0);
/* */ }
/* */
/* */
/* */ public String toString() {
/* 78 */ return "stack: " + this.list.toString();
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch19/GenericStack2.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/