KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > IntList2d


1 package org.apache.poi.util;
2
3 import java.util.List JavaDoc;
4 import java.util.ArrayList JavaDoc;
5
6 /**
7  * Provides an interface for interacting with 2d arrays of integers. This
8  * implementation will return 0 for items not yet allocated and automatically
9  * increase the array size for set operations. You never get an index out of
10  * bounds.
11  *
12  * @author Glen Stampoultzis (glens at apache.org)
13  * @version $Id: IntList2d.java,v 1.1 2004/04/18 13:02:48 glens Exp $
14  */

15 public class IntList2d
16 {
17     // Implemented using a List of IntList's.
18
List JavaDoc rows = new ArrayList JavaDoc();
19
20     public int get(int col, int row)
21     {
22         if (row >= rows.size())
23         {
24             return 0;
25         }
26         else
27         {
28             IntList cols = (IntList) rows.get(row);
29             if (col >= cols.size())
30                 return 0;
31             else
32                 return cols.get( col );
33         }
34     }
35
36     public void set(int col, int row, int value)
37     {
38         resizeRows(row);
39         resizeCols(row,col);
40         IntList cols = (IntList) rows.get( row );
41         cols.set( col, value );
42     }
43
44     private void resizeRows( int row )
45     {
46         while (rows.size() <= row)
47             rows.add( new IntList() );
48     }
49
50     private void resizeCols( int row, int col )
51     {
52         IntList cols = (IntList) rows.get( row );
53         while (cols.size() <= col)
54             cols.add(0);
55     }
56
57     public boolean isAllocated( int col, int row )
58     {
59         if (row < rows.size())
60         {
61             IntList cols = (IntList) rows.get( row );
62             return ( col < cols.size() );
63         }
64         else
65         {
66             return false;
67         }
68     }
69
70
71
72 }
73
Popular Tags