KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > util > IntegerArray


1 /*
2  * IntegerArray.java - Automatically growing array of ints
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2001 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * 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 {
30     //{{{ IntegerArray constructor
31
public IntegerArray()
32     {
33         this(2000);
34     } //}}}
35

36     //{{{ IntegerArray constructor
37
public IntegerArray(int initialSize)
38     {
39         array = new int[initialSize];
40     } //}}}
41

42     //{{{ add() method
43
public void add(int num)
44     {
45         if(len >= array.length)
46         {
47             int[] arrayN = new int[len * 2];
48             System.arraycopy(array,0,arrayN,0,len);
49             array = arrayN;
50         }
51
52         array[len++] = num;
53     } //}}}
54

55     //{{{ get() method
56
public final int get(int index)
57     {
58         return array[index];
59     } //}}}
60

61     //{{{ getSize() method
62
public final int getSize()
63     {
64         return len;
65     } //}}}
66

67     //{{{ setSize() method
68
public final void setSize(int len)
69     {
70         this.len = len;
71     } //}}}
72

73     //{{{ clear() method
74
public final void clear()
75     {
76         len = 0;
77     } //}}}
78

79     //{{{ getArray() method
80
public int[] getArray()
81     {
82         return array;
83     } //}}}
84

85     //{{{ Private members
86
private int[] array;
87     private int len;
88     //}}}
89
}
90
Popular Tags