

PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
java.lang.Object tribble.util.GridList2D
public class GridList2D
Provides methods for arranging 1dimensional array elements into a 2dimensional grid.
Consider a 1dimensional array containing 14 elements, as shown here:
0  1  2  3  4  5  6  7  8  9  10  11  12  13 

We wish to display the elements in a 2dimensional grid with four columns, so that the elements are arranged in order from top to bottom down each row, and then from left to right across rows. Any leftover cells in the grid that do not correspond to any elements in the array are left empty. Every cell in each row, except possibly the last row, are filled; any remaining unfilled cells will be the rightmost cells of the last row. The diagram below illustrates:
Columns (x)  

Rows (y) 

Note that cells within the 2dimensional grid that do not have a corresponding element in the 1dimensional source array, as well as cells outside the grid, have an index of −1.
Determing the array indices for each grid cell is accomplished with code similar to the following:
int nCols = 4;
boolean printed = true;
for (int y = 0; printed; y++)
{
printed = false;
for (int x = 0; x < nCols; x++)
{
int i;
i = GridList2D.indexFor
(x, y, nCols, arr.length);
if (i < 0)
break;
out.print(arr[i]);
printed = true;
}
if (printed)
out.println();
}
An alternative approach is to initialize a GridList2D object with the size of the 2dimensional grid and the length of the 1dimensional source array:
int nCols = 4;
GridList2D grid;
grid = new GridList2D
(nCols, arr.length);
The indexFor() method of the object can then be called for each grid cell coordinate to determine its corresponding index in the source array:
boolean printed = true;
for (int y = 0; printed; y++)
{
printed = false;
for (int x = 0; ; x++)
{
int i;
i = grid.indexFor
(x, y);
if (i < 0)
break;
out.print(arr[i]);
printed = true;
}
if (printed)
out.println();
}
Copyright ©2010 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated by
by the United States Department of State as a terrorist, or terrorist
government or agency, to use and distribute this source code provided
that the original copyright notice remains present and unaltered.
Constructor Summary  

GridList2D(int cols,
int elems)
Construct an object to determine the indices of the elements in a 1dimensional array that are located at particular (x, y) coordinates within a 2dimensional grid. 
Method Summary  

int 
getArrayLength()
Retrieve the number of elements in the 1dimensional source array. 
int 
getColumns()
Retrieve the number of columns in the 2dimensional grid. 
int 
getRows()
Retrieve the number of rows in the 2dimensional grid. 
int 
indexFor(int x,
int y)
Determine the index of an element in a 1dimensional array that is located at a particular (x, y) cell coordinate within a 2dimensional grid. 
static int 
indexFor(int x,
int y,
int cols,
int elems)
Determine the index of an element in a 1dimensional array that is located at a particular (x, y) coordinate within a 2dimensional grid. 
Methods inherited from class java.lang.Object 

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait 
Constructor Detail 

public GridList2D(int cols, int elems)
cols
 Width of the 2dimensional grid, i.e., the number of columns it contains.elems
 Number of elements in the 1dimensional array.Method Detail 

public static int indexFor(int x, int y, int cols, int elems)
x
 X (horizontal) coordinate within the 2dimensional grid.y
 Y (vertical) coordinate within the 2dimensional grid.cols
 Width of the 2dimensional grid, i.e., the number of columns it contains.elems
 Number of elements in the 1dimensional array.
public int indexFor(int x, int y)
x
 X (horizontal) coordinate within the 2dimensional grid.y
 Y (vertical) coordinate within the 2dimensional grid.
public int getColumns()
public int getRows()
public int getArrayLength()


PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 