01: /*
02: * IntegerArray.java - Automatically growing array of ints
03: * :tabSize=8:indentSize=8:noTabs=false:
04: * :folding=explicit:collapseFolds=1:
05: *
06: * Copyright (C) 2001 Slava Pestov
07: *
08: * This program is free software; you can redistribute it and/or
09: * modify it under the terms of the GNU General Public License
10: * as published by the Free Software Foundation; either version 2
11: * of the License, or any later version.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: * You should have received a copy of the GNU General Public License
19: * along with this program; if not, write to the Free Software
20: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21: */
22:
23: package org.gjt.sp.util;
24:
25: /**
26: * A simple collection that stores integers and grows automatically.
27: */
28: public class IntegerArray {
29: //{{{ IntegerArray constructor
30: public IntegerArray() {
31: this (2000);
32: } //}}}
33:
34: //{{{ IntegerArray constructor
35: public IntegerArray(int initialSize) {
36: array = new int[initialSize];
37: } //}}}
38:
39: //{{{ add() method
40: public void add(int num) {
41: if (len >= array.length) {
42: int[] arrayN = new int[len * 2];
43: System.arraycopy(array, 0, arrayN, 0, len);
44: array = arrayN;
45: }
46:
47: array[len++] = num;
48: } //}}}
49:
50: //{{{ get() method
51: public final int get(int index) {
52: return array[index];
53: } //}}}
54:
55: //{{{ getSize() method
56: public final int getSize() {
57: return len;
58: } //}}}
59:
60: //{{{ setSize() method
61: public final void setSize(int len) {
62: this .len = len;
63: } //}}}
64:
65: //{{{ clear() method
66: public final void clear() {
67: len = 0;
68: } //}}}
69:
70: //{{{ getArray() method
71: public int[] getArray() {
72: return array;
73: } //}}}
74:
75: //{{{ Private members
76: private int[] array;
77: private int len;
78: //}}}
79: }
|