#include "javatech_jni22_ArrayExample.h" JNIEXPORT jfloat JNICALL Java_javatech_jni22_ArrayExample_nativeProcessArray (JNIEnv *jenv, jobject, jfloatArray the_jarray, jint j_index) { float* c_array = jenv->GetFloatArrayElements (the_jarray, NULL); c_array[3] = -999.f; int c_index = j_index; float element = c_array[c_index]; jenv->ReleaseFloatArrayElements (the_jarray, c_array, 0); jfloat jelement = element; return jelement; } // nativeProcessArray JNIEXPORT jint JNICALL Java_javatech_jni22_ArrayExample_nativeProcessArrayRegion (JNIEnv *jenv, jobject, jintArray the_jarray) { jint region[10]; jint sum = 0; jenv->GetIntArrayRegion (the_jarray, 5, 10, region); for (int i=0; i < 10; i++) sum += region[i]; return sum; } // nativeProcessArrayRegion JNIEXPORT void JNICALL Java_javatech_jni22_ArrayExample_nativeModifyArrayRegion (JNIEnv *jenv, jobject, jintArray the_jarray) { jint region[10]; for (int i=0; i < 10; i++) region[i] = -5 - i; jenv->SetIntArrayRegion (the_jarray, 5, 10, region); } // nativeProcessArrayRegion JNIEXPORT void JNICALL Java_javatech_jni22_ArrayExample_nativeProcess2DArray (JNIEnv *jenv, jobject, jobjectArray joa2d) { // Get the length of the 2D array jint len1 = jenv->GetArrayLength (joa2d); // Extract the 0-th element from the 2D array and cast into a jintArray jintArray i2d_0 = (jintArray) jenv->GetObjectArrayElement (joa2d, 0); // Get the length of the extracted 1D array jint len2 = jenv->GetArrayLength (i2d_0); // Allocate a buffer to receive a copy of the elements from the 1D array jint buffer[4]; // Copy the all of the elements of the entire 1D array into the buffer jenv->GetIntArrayRegion (i2d_0, 0, 4, buffer); // Modify them in some way. for (int i=0; i < 4; i++) buffer[i]*=2; // Insert the modified buffer back into the 1D array. jenv->SetIntArrayRegion (i2d_0, 0, 4, buffer); } // nativeProcessArrayRegion