KickJava   Java API By Example, From Geeks To Geeks.

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


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 objects. This
8  * implementation will return null 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: List2d.java,v 1.1 2004/04/18 13:02:48 glens Exp $
14  */

15 public class List2d
16 {
17     // Implemented using a List of List's.
18
List JavaDoc rows = new ArrayList JavaDoc();
19
20     public Object JavaDoc get(int col, int row)
21     {
22         if (row >= rows.size())
23         {
24             return null;
25         }
26         else
27         {
28             List JavaDoc cols = (List JavaDoc) rows.get(row);
29             if (col >= cols.size())
30                 return null;
31             else
32                 return cols.get( col );
33         }
34     }
35
36     public void set(int col, int row, Object JavaDoc value)
37     {
38         resizeRows(row);
39         resizeCols(row,col);
40         List JavaDoc cols = (List JavaDoc) 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 ArrayList JavaDoc() );
48     }
49
50     private void resizeCols( int row, int col )
51     {
52         List JavaDoc cols = (List JavaDoc) rows.get( row );
53         while (cols.size() <= col)
54             cols.add(null);
55     }
56
57
58 }
59
Popular Tags