This page was last modified 1-Mar-2002.

prev | TOC | next

Passing Arrays to JavaBeans

To pass an array from JTransit to a JavaBean, the best way is to use the same class used by JTransit to store arrays internally: jtransit.tags.TagResultArray. The following code shows how to use it. (download source code)

import jtransit.tags.TagResultArray;

public class ArrayInfoBean
{
	private TagResultArray array;

	public void setArrayValue( TagResultArray array )
	{
		// This line allows the original JTransit array
		// to be modified:
		//
		this.array = array;
		//
		// If you want to make sure the original array can't be
		// modified, use the following code instead (it's slower
		// because it creates a copy of the array):
		//
		// this.array = new TagResultArray( array );
	}

	public int getNumCols()
	{
		return array.getNumCols();
	}

	public int getNumRows()
	{
		return array.getNumRows();
	}

	public double getAverage()
	{
		return array.average();
	}

	public double getMax()
	{
		return array.max();
	}

	public double getMin()
	{
		return array.min();
	}

	public void doStuff()
	{
		// array.getCell( int row, int col )
		// or array.getCell( int row, String colName ) will return data
		//    (row and col start with 1)
	}
}

You can also use method parameters of type Object[][] and String[][] to accept arrays from JTransit. Object[][] is the fastest, and allows each cell of the array to retain the same type it has in JTransit (ie dates, numeric, etc.). You can call toString() on the cell contents to convert each one into a String if desired. When you declare a method parameter type of String[][] in your bean, JTransit must call toString() on every cell of the TagResultArray and construct a new, temporary String[][] to hold all the array data, which can take a lot of time and memory.

The only good thing about using String[][] to accept array data is that the array is completely copied and therefore the source array data cannot be modified by the JavaBean. When passing a TagResultArray, you should be careful not to modify the array (by calling setCell, setNumRows, setNumCols, insertRow, deleteRow, etc.), because the array is passed by "reference" and changing it will affect the original array in JTransit as well. This might be what you want, but in general, it is not. You can uncomment the line this.array = new TagResultArray(array) in the code sample above if you want to make sure the bean doesn't modify the original array. When passing arrays using Object[][], the structure of the original array cannot be changed, but individual cells will be able to be changed by changing the contents of the Object[][] by assignment (ie, array[0][0] = "foo";).


prev | TOC | next
 Join the JTransit Talk List   |   Email JTransit Support   |   Visit http://jtransit.com/
Copyright ©2002 JTransit, Inc., All Rights Reserved
JTransit, JTransit Compiler, JTransit Runtime, FrontLine and RedLine are Trademarks of JTransit, Inc.
ColdFusion is a trademark of Allaire Corp. Tango is a trademark of Pervasive Software, Inc. WiTango is a trademark of With Enterprise Pty Ltd. Other trademarks are property of the respective trademark owners.