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; i<SIZE1; i++)
  {
  for (int j=0; j<SIZE2; j++)
  {
  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
   
  ; The Java class array2d creates 2 initial arrays, one
  ; of longs and one of shorts. We can interrogate and
  ; change this array.
  oJArr = OBJ_NEW('IDLJavaObject$ARRAY2D', 'array2d')
   
  ; First, let’s see what is in the short array at index
  ; (2,3).
  PRINT, 'array2d short(2, 3) = ', $
  oJArr -> GetShortByIndex(2, 3), $
  '	(should be 23)’
   
  ; Now, let’s copy the entire array from Java to IDL.
  shortArrIDL = oJArr->GetShorts()
  HELP, shortArrIDL
  PRINT, 'shortArrIDL[2, 3] = ', shortArrIDL[2, 3], $
  '	(should be 23)'
   
  ; Let’s change this value... 
  shortArrIDL[2, 3] = 999
   
  ; ...and copy it back to Java...
  oJArr->SetShorts, shortArrIDL
   
  ; ...now its value should be different.
  PRINT, 'array2d short(2, 3) = ', $
  oJArr->GetShortByIndex(2, 3), '	(should be 999)'
   
  ; Let’s set our array to something different.
  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)'
   
  ; Array2d has a setLongs method, but b/c arrays do not
  ; (currently) promote, the first call to setLongs works
  ; but the second fails.
  oJArr->SetLongs, L64INDGEN(10, 8)
  PRINT, 'array2d long(0, 1) = ', $
  oJArr->GetLongByIndex(0, 1), '	(should be 10)'
   
  ;PRINT, '(expecting an error on the next line...)'
  ;oJArr->SetLongs, INDGEN(10,8)
   
  ; Cleanup our object.
  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)