Lesson 9: Using Array Proxy Types
Introduction
Arrays are fairly straightforward to use, once you remember that proxy arrays are not .NET arrays. .NET does not allow a type to extend that .NET Array type, which makes it impossible for us to make proxy array types full .NET arrays. The best we can do is to create types that are usable as if they were .NET array types (at least largely).
The biggest differences between real .NET arrays and proxy arrays can be found in the following areas:
- Proxy array types have a special naming convention that you just need to know (shown below).
- You cannot instantiate proxy arrays instances using the rectangular bracket syntax, you have to use constructor syntax instead.
- You cannot use proxy array instances in places where .NET would normally expect an array instance.
On the other hand, leaving these limitations aside, everything else works just as expected:
- You can use
.Length
on proxy array instances. - Y>ou can use subscript operators for element access.
- You can use the properties that the .NET framework normally defines for Array instances.
- You can use
foreach
on proxy arrays instances.
Here is an example of how you might use a one-dimensional proxy array for the java.lang.String
type
with space for 4 string elements:
StringArray arrStr = new StringArray( 4 ); Console.WriteLine( "The array size is {0}", arrStr.Length ); arrStr[ 0 ] = "test"; arrStr[ 1 ] = "value"; arrStr[ 2 ] = null; arrStr[ 3 ] = "..."; foreach( string s in arrStr ) Console.WriteLine( "Element: {0}", s != null ? s : "(null)" );
Array Type Names
The following table gives you the overview of how Java and .NET proxy array type names are mapped:
boolean[]
Codemesh.JuggerNET.boolArray
byte[]
Codemesh.JuggerNET.byteArray
char[]
Codemesh.JuggerNET.charArray
double[]
Codemesh.JuggerNET.doubleArray
float[]
Codemesh.JuggerNET.floatArray
int[]
Codemesh.JuggerNET.intArray
long[]
Codemesh.JuggerNET.longArray
short[]
Codemesh.JuggerNET.shortArray
java.lang.Object[]
Java.Lang.ObjectArray
java.lang.Object[][]
Java.Lang.ObjectArrayArray
java.util.Map[]
Java.Util.MapArray
Please note that the type names for arrays of reference-type elements are in the same namespaces as their elements.