Working with Java Arrays
Description
It's always been possible to interact with
Java array objects in BBj using Java Array reflection:
use java.awt.Color use java.lang.reflect.Array color! = Array.newInstance(Class.forName("java.awt.Color"),2)
Array.set(color!,0,Color.RED) Array.set(color!,1,Color.BLUE) print "color![0]=",Array.get(color!,0) print "color![1]=",Array.get(color!,1)
|
In BBj 18.0 and higher, this functionality
can be expressed in a more natural syntax:
use java.awt.Color declare Color[] Color! color! = new Color[2] color![0] = Color.RED color![1] = Color.BLUE print "color![0]=",color![0] print "color![1]=",color![1]
|
It's legal (but strongly discouraged) for
a traditional BBj object array X![]and a Java array object X!to coexist.
The syntax for referencing Java array elements is identical to the syntax
for referencing BBj array elements, and in the event of a conflict, the
traditional BBj array always wins, hiding the Java array object. (The
Java array object can always be accessed using the reflection strategy
described at the beginning of this page, but it's preferable to avoid
conflicting variable names.)
READY >/ rem ' Resolving conflicting BBj and Java array references data "Java","object","array","x!" print "Create Java array x!" x! = new Object[4] dread x![] print "x![0]=",x![0] data "BBj","object","array","x![]" print "Create BBj array x![]" dim x![0:3] dread x![] print "x![0]=",x![0] dump (0,mode="vars") print "Clear BBj array x![]" clear x![] print "x![0]=",x![0] print "Clear Java array x!" clear x! print "x![0]=",x![0] stop >run Create Java array x! x![0]=Java Create BBj array x![] x![0]=BBj ****** LEVEL 0, PGM=dup.txt x! (java.lang.Object[]) has 4 elements [0] len=4 1: "Java" $4A617661$ [1] len=6 1: "object" $6F626A6563 74$ [2] len=5 1: "array" $6172726179$ [3] len=2 1: "x!" $7821$ x![] has 4 elements [3] [0] len=3 1: "BBj" $42426A$ [1] len=6 1: "object" $6F626A6563 74$ [2] len=5 1: "array" $6172726179$ [3] len=4 1: "x![]" $78215B5D$ Clear BBj array x![] x![0]=Java Clear Java array x! !ERROR=42 (Array is not defined) [18] print "x![0]=",x![0] READY >
|
Java primitive and object arrays can be
defined on the server or client.
READY >/ declare int[] i! i! = new int[2] print i!.getClass().getTypeName(),i!.length i![1] = 42 declare Double[] d! d! = new Double[3] print d!.getClass().getTypeName(),d!.length d![1] = Math.PI declare byte@[] b! b! = new byte@[4] print b!.getClass().getTypeName(),b!.length b![2] = 127 dump (0,mode="vars") >run int[] 2 java.lang.Double[] 3 byte[] 4 ****** LEVEL 0, PGM=arrays.txt i! (int[]) has 2 elements [0] = 0 [1] = 42 d! (java.lang.Double[]) has 3 elements [0] = null [1] java.lang.Double 1: "3.141592653589793" $332E313431 3539323635 3335383937 3933$ [2] = null b! (byte@[]) has 4 elements [0] = 0 [1] = 0 [2] = 127 [3] = 0 READY >
|
When interacting with Java array elements,
primitive types are converted as described in Calling
Java From BBj.
READY >x! = new int[10] >x![1] = 5 >x![2] = 3.2 !ERROR=26 (3.2 could not be converted to int) >x! = new boolean[10] >x![1] = 0 >x![2] = 1 >x![3] = Boolean.TRUE >x![4] = "oops" !ERROR=42 (argument type mismatch) >x! = new Boolean[10] >x![1] = 0 !ERROR=42 (array element type mismatch) >x![1] = Boolean.TRUE >x![2] = Boolean.FALSE >
|
Most references to array![]or array![all] will resolve to the traditional BBj array
if one exists by that name, otherwise the interpreter will look for a
Java array object. Some references require that you specify one or the
other. The first such case is the DIMS()function:
READY >/ begin dim dims$:"dimensions:i(1),dim0elem:i(4),dim0base:i(4),dim1elem:i(4),dim1base:i(4),dim2 elem:i(4),dim2base:i(4)" print "Java Array (x! = new String[5]): dims(x!)" restore 0 x! = new String[5] dread x![] dims$ = dims(x!) gosub dims print "Java Client Array (x! = new String@[5]): dims(x!)" restore 0 x! = new String@[5] dread x![] dims$ = dims(x!) gosub dims dim x![1:5] restore 0 dread x![] print "BBx Array (dim x![1:5]): dims(x![])" dims$ = dims(x![]) gosub dims stop data "Steve","Peggy","Jason","Tanner","Andy" dims: for x=dims.dim0base to dims.dim0elem+dims.dim0base-1 print "x! element",x," = ",x![x] next x return >run Java Array (x! = new String[5]): dims(x!) x! element 0 = Steve x! element 1 = Peggy x! element 2 = Jason x! element 3 = Tanner x! element 4 = Andy Java Client Array (x! = new String@[5]): dims(x!) x! element 0 = Steve x! element 1 = Peggy x! element 2 = Jason x! element 3 = Tanner x! element 4 = Andy BBx Array (dim x![1:5]): dims(x![]) x! element 1 = Steve x! element 2 = Peggy x! element 3 = Jason x! element 4 = Tanner x! element 5 = Andy READY >
|
Array references in CALL/ENTER must also distinguish between
BBj array references (array![])
and Java array references (array!):
READY >load "callarray.txt" READY >/ restore 0 dim x![3] dread x![] print "Call with BBx array: ",x![] call pgm(-2)+"::array",x![] restore 0 y! = new int[4] dread y![] print "Call with Java array object: ",y![] call pgm(-2)+"::object",y! stop data 0,1,2,3 array: enter array![] print array![] exit object: enter object! print object![] exit >run Call with BBx array: 0 1 2 3 0 1 2 3 Call with Java array object: 0123 0123 READY >
|
See Also
BBj
Object Variables
Calling
Java From BBj
CALL
Verb - BBj-Specific Information
DECLARE
Verb
ENTER
Verb - BBj-Specific Information
LET
Verb - BBj-Specific Information
NULL()
Function