annotate src/example2/MyStack.java @ 1:0a4fdcd1ed46 default tip

minor change
author e095732
date Tue, 29 Jan 2013 15:27:43 +0900
parents 4dbea8e22628
children
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
1
0a4fdcd1ed46 minor change
e095732
parents: 0
diff changeset
3 /**
0a4fdcd1ed46 minor change
e095732
parents: 0
diff changeset
4 * 実行させてもMemory Leakはしません
0a4fdcd1ed46 minor change
e095732
parents: 0
diff changeset
5 * 参照が残り続けるStackなのでたくさん生成するといつかMemory Leakを起こす
0a4fdcd1ed46 minor change
e095732
parents: 0
diff changeset
6 */
0a4fdcd1ed46 minor change
e095732
parents: 0
diff changeset
7
0
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
8 public class MyStack {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
9 private Object[] stack;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
10 private int size = 0;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
11 private static final int MAX_SIZE = 16;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
12
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
13 public MyStack() {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
14 this.stack = new Object[MAX_SIZE];
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 void push(Object e) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
18 if (size==MAX_SIZE) return;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
19 stack[size++] = e;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
20 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
21
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
22 public Object pop() {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
23 if (size == 0) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
24 return null;
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
25 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
26 return stack[--size];
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
27 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
28
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
29 private void print() {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
30 for (Object o : this.stack) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
31 if (o == null) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
32 System.out.print("n ");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
33 } else {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
34 System.out.print(o + " ");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
35 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
36 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
37 System.out.println();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
38 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
39
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
40 public static void main(String[] args) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
41 MyStack s = new MyStack();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
42
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
43 System.out.print("First:");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
44 s.print();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
45
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
46 for (int i = 0; i < 10; i++) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
47 s.push(Integer.valueOf(i)); // push
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
48 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
49
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
50 System.out.print("push: ");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
51 s.print();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
52
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
53 for (int i = 0; i < 10; i++) {
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
54 s.pop();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
55 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
56
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
57 System.out.print("pop: ");
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
58 s.print();
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
59 }
4dbea8e22628 Memory Leak TEST
e095732
parents:
diff changeset
60 }