KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > beaver > comp > util > IntArray


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * This file is part of Beaver Parser Generator. *
3  * Copyright (C) 2003,2004 Alexander Demenchuk <alder@softanvil.com>. *
4  * All rights reserved. *
5  * See the file "LICENSE" for the terms and conditions for copying, *
6  * distribution and modification of Beaver. *
7  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

8
9 package beaver.comp.util;
10
11 /**
12  *
13  */

14 public class IntArray
15 {
16     private int[] data;
17     private int size;
18     
19     public IntArray(int capacity)
20     {
21         data = new int[capacity];
22     }
23     
24     public IntArray()
25     {
26         this(16);
27     }
28     
29     public void add(int value)
30     {
31         if (size == data.length)
32         {
33             int[] tmp = new int[size * 2];
34             System.arraycopy(data, 0, tmp, 0, size);
35             data = tmp;
36         }
37         data[size++] = value;
38     }
39     
40     public void compact()
41     {
42         if (size < data.length)
43         {
44             int[] tmp = new int[size];
45             System.arraycopy(data, 0, tmp, 0, size);
46             data = tmp;
47         }
48     }
49
50     public int get(int index)
51     {
52         return data[index];
53     }
54     
55     public int size()
56     {
57         return size;
58     }
59 }
60
Popular Tags