This example creates a two-dimensional array within a Java class, which is contained in a file named array2d.java. IDL then accesses this data through the ArrayDemo routine, which is in a file named arraydemo.pro.
Note: These files are located in the resource/bridges/import/java/examples directory of the IDL distribution. Run this example procedure by entering arraydemo at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT arraydemo.pro.
The array2d.java file contains the following text for creating a two-dimensional array in Java:
public class array2d
{
short[][] m_as
long[][] m_aj
// ctor
public array2d()
{
int SIZE1 = 3
int SIZE2 = 4
// default ctor creates a fixed number of elements m_as = new short[SIZE1][SIZE2]
m_aj = new long[SIZE1][SIZE2]
for (int i=0
{
for (int j=0
{
m_as[i][j] = (short)(i*10+j)
m_aj[i][j] = (long)(i*10+j)
}
}
}
public void setShorts(short[][] _as)
{
m_as = _as
}
public short[][] getShorts()
{
return m_as
}
public short getShortByIndex(int i, int j)
{
return m_as[i][j]
}
public void setLongs(long[][] _aj)
{
m_aj = _aj
}
public long[][] getLongs()
{
return m_aj
}
public long getLongByIndex(int i, int j)
{
return m_aj[i][j]
}
}
The arraydemo.pro file contains the following text for accessing the two- dimensional array within IDL:
PRO ArrayDemo
oJArr = OBJ_NEW('IDLJavaObject$ARRAY2D', 'array2d')
PRINT, 'array2d short(2, 3) = ', $
oJArr -> GetShortByIndex(2, 3), $
' (should be 23)’
shortArrIDL = oJArr->GetShorts()
HELP, shortArrIDL
PRINT, 'shortArrIDL[2, 3] = ', shortArrIDL[2, 3], $
' (should be 23)'
shortArrIDL[2, 3] = 999
oJArr->SetShorts, shortArrIDL
PRINT, 'array2d short(2, 3) = ', $
oJArr->GetShortByIndex(2, 3), ' (should be 999)'
oJArr -> SetShorts, INDGEN(10, 8)
PRINT, 'array2d short(0, 0) = ', $
oJArr->GetShortByIndex(0, 0), ' (should be 0)'
PRINT, 'array2d short(1, 0) = ', $
oJArr->GetShortByIndex(1, 0), ' (should be 1)'
PRINT, 'array2d short(2, 0) = ', $
oJArr->GetShortByIndex(2, 0), ' (should be 2)'
PRINT, 'array2d short(0, 1) = ', $
oJArr->GetShortByIndex(0, 1), ' (should be 10)'
oJArr->SetLongs, L64INDGEN(10, 8)
PRINT, 'array2d long(0, 1) = ', $
oJArr->GetLongByIndex(0, 1), ' (should be 10)'
OBJ_DESTROY, oJArr
END
After saving and compiling the above files (array2d.java in Java and ArrayDemo.pro in IDL), update the jbexamples.jar file in the resource/bridges/import/java directory with the new compiled class and run the ArrayDemo routine in IDL. The routine should produce the following results:
array2d short(2, 3) = 23 (should be 23)
SHORTARRIDL INT = Array[3, 4]
shortArrIDL[2, 3] = 23 (should be 23)
array2d short(2, 3) = 999 (should be 999)
array2d short(0, 0) = 0 (should be 0)
array2d short(1, 0) = 1 (should be 1)
array2d short(2, 0) = 2 (should be 2)
array2d short(0, 1) = 10 (should be 10)
array2d long(0, 1) = 10 (should be 10)