While in many ways Java is similar to C and C++, it is quite different
in its treatment of arrays. C arrays are based on the idea of pointer
arithmetic, which would be incompatible with Java's security
requirements. Java arrays are true objects (array types inherit from
java.lang.Object). An array-valued variable is one that
contains a reference (pointer) to an array object.
Referencing a Java array in C++ code is done using the
JArray template, which as defined as follows:
class __JArray : public java::lang::Object
{
public:
int length;
};
template<class T>
class JArray : public __JArray
{
T data[0];
public:
T& operator[](jint i) { return data[i]; }
};
There are a number of typedefs which correspond to typedefs
from the JNI. Each is the type of an array holding objects
of the relevant type:
typedef __JArray *jarray;
typedef JArray<jobject> *jobjectArray;
typedef JArray<jboolean> *jbooleanArray;
typedef JArray<jbyte> *jbyteArray;
typedef JArray<jchar> *jcharArray;
typedef JArray<jshort> *jshortArray;
typedef JArray<jint> *jintArray;
typedef JArray<jlong> *jlongArray;
typedef JArray<jfloat> *jfloatArray;
typedef JArray<jdouble> *jdoubleArray;
| T* elements (JArray |
Method on template<class T> |
This template function can be used to get a pointer to the elements of
the array. For instance, you can fetch a pointer to the
integers that make up an int[] like so:
extern jintArray foo;
jint *intp = elements (foo);
The name of this function may change in the future. |
| jobjectArray JvNewObjectArray (jsize length, jclass klass, jobject init) | Function |
Here klass is the type of elements of the array and
init is the initial value put into every slot in the array.
|
For each primitive type there is a function which can be used to create a new array of that type. The name of the function is of the form:
JvNewTypeArray
For example:
JvNewBooleanArray
can be used to create an array of Java primitive boolean types.
The following function definition is the template for all such functions:
| jbooleanArray JvNewBooleanArray (jint length) | Function |
| Create's an array length indices long. |
| jsize JvGetArrayLength (jarray array) | Function |
| Returns the length of the array. |