annotate src/example2/MyStack.java @ 0:4dbea8e22628

Memory Leak TEST
author e095732
date Mon, 28 Jan 2013 22:05:30 +0900
parents
children 0a4fdcd1ed46
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
1 package example2;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
2
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
3 public class MyStack {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
4 private Object[] stack;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
5 private int size = 0;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
6 private static final int MAX_SIZE = 16;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
7
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
8 public MyStack() {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
9 this.stack = new Object[MAX_SIZE];
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
10 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
11
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
12 public void push(Object e) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
13 if (size==MAX_SIZE) return;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
14 stack[size++] = e;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
15 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
16
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
17 public Object pop() {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
18 if (size == 0) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
19 return null;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
20 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
21 return stack[--size];
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
22 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
23
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
24 private void print() {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
25 for (Object o : this.stack) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
26 if (o == null) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
27 System.out.print("n ");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
28 } else {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
29 System.out.print(o + " ");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
30 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
31 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
32 System.out.println();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
33 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
34
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
35 public static void main(String[] args) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
36 MyStack s = new MyStack();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
37
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
38 System.out.print("First:");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
39 s.print();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
40
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
41 for (int i = 0; i < 10; i++) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
42 s.push(Integer.valueOf(i)); // push
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
43 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
44
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
45 System.out.print("push: ");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
46 s.print();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
47
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
48 for (int i = 0; i < 10; i++) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
49 s.pop();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
50 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
51
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
52 System.out.print("pop: ");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
53 s.print();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
54 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
55 }