#include <jni.h>#include "IntArray.h"JNIEXPORT jint JNICALL Java_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr){ jsize len = (*env)->GetArrayLength(env, arr); int i, sum = 0; jint *body = (*env)->GetIntArrayElements(env, arr, 0); for (i=0; i<len; i++) { sum += body[i]; } (*env)->ReleaseIntArrayElements(env, arr, body, 0); return sum;}
class IntArray { private native int[] sumArray(int arr[]); public static void main(String args[]) { IntArray p = new IntArray(); int arr[] = new int [10]; for (int i = 0; i < 10; i++) arr[i] = i; int[] sum = p.sumArray(arr); for(int i = 0;i<10;i++) { System.out.println("sum = " + sum); } } static { System.loadLibrary("MyImpOfIntArray"); }}