Alex Rivera | Logout

Java JNI - Is it possible to set an individual primitive array element in Java from C++

Asked 2010-09-09T15:48:20.283
10

Basically, I've been doing the following to retrieve Java Instance Fields (in this case, an int) and setting it to a new value like the following:

jclass cls = env->GetObjectClass(obj);
jfieldID fid = env->GetFieldID(cls, "myVariable", "I");
env->SetIntField(obj, fid, (jint)2012);

However, I'd like to do this for an individual int element in a java int array such that:

jclass cls = env->GetObjectClass(obj);
jfieldID fid = env->GetFieldID(cls, "myVariableArray", "[I");
PSUDOCODE: <"SET myVariableArray[0] = 2013" ... Is there a method for this?>

Is there such a thing?

Edit
Report

1 Answer

10

I found the answer after looking through 15+ documents.

// Grab Fields
jclass cls = env->GetObjectClass(obj);
jfieldID fid = env->GetFieldID(cls, "testField", "[I");

jintArray jary;
jary = (jintArray)env->GetObjectField(obj, fid);
jint *body = env->GetIntArrayElements(jary, 0);
body[0] = 3000;
env->ReleaseIntArrayElements(jary, body, 0);

ReleaseIntArrayElements is key ... it returns a copy back to the java Instance Variable.

answered 2010-09-09T17:22:47.483

Your Answer