15.2.2.4 传递整型数组
来源:互联网 发布:linux改语言环境 编辑:程序博客网 时间:2024/05/20 21:18
public class Sample2{ public native int[] intMethod(); public static void main(String[] args) { System.loadLibrary("Sample2"); Sample2 sample=new Sample2(); int[] nums=sample.intMethod(); for(int i=0;i<nums.length;i++) System.out.println(nums[i]); }}/*// Sample2.cpp : 定义 DLL 应用程序的导出函数。//#include "stdafx.h"#include "..\Sample2.h"JNIEXPORT jintArray JNICALL Java_Sample2_intMethod (JNIEnv *env, jobject obj){ jint i = 1; jintArray array;//定义数组对象 array = env-> NewIntArray(10); for(; i<= 10; i++) env->SetIntArrayRegion( array, i-1, 1, &i); // 获取数组对象的元素个数 int len = env->GetArrayLength( array); // 获取数组中的所有元素 jboolean iscopy; jint* elems = env-> GetIntArrayElements(array, &iscopy); if(iscopy) printf("iscopy is true\n"); else printf("iscopy is false\n"); for(i=0; i<len; i++) printf("ELEMENT %d IS %d\n", i, elems[i]); return array;}*//*iscopy is trueELEMENT 0 IS 1ELEMENT 1 IS 2ELEMENT 2 IS 3ELEMENT 3 IS 4ELEMENT 4 IS 5ELEMENT 5 IS 6ELEMENT 6 IS 7ELEMENT 7 IS 8ELEMENT 8 IS 9ELEMENT 9 IS 1012345678910请按任意键继续. . .*/
public class Sample2{ public native int[] intMethod(); public static void main(String[] args) { System.loadLibrary("Sample2"); Sample2 sample=new Sample2(); int[] nums=sample.intMethod(); for(int i=0;i<nums.length;i++) System.out.println(nums[i]); }}/*// Sample2.cpp : 定义 DLL 应用程序的导出函数。//#include "stdafx.h"#include "..\Sample2.h"JNIEXPORT jintArray JNICALL Java_Sample2_intMethod (JNIEnv *env, jobject obj){ jint i = 1; jintArray array;//定义数组对象 array = env-> NewIntArray(10); for(; i<= 10; i++) env->SetIntArrayRegion( array, i-1, 1, &i); // 获取数组对象的元素个数 int len = env->GetArrayLength( array); // 获取数组中的所有元素 jboolean iscopy; jint* elems = env-> GetIntArrayElements(array, &iscopy); if(iscopy) printf("iscopy is true\n"); else printf("iscopy is false\n"); for(i=0; i<len; i++) printf("ELEMENT %d IS %d\n", i, elems[i]); for (int i=0; i<len; i++) elems[i]+=1; env->ReleaseIntArrayElements(array,elems,0);//释放~~~ // 对于最后一个参数(如果指针指向的数组为副本时,否则该参数不起作用) // 0 copy back the content and free the elems buffer // JNI_COMMIT copy back the content but do not free the elems buffer // JNI_ABORT free the buffer without copying back the possible changes if(iscopy) printf("iscopy is true,copy back the content and free the elems buffer\n");return array;}*//*iscopy is trueELEMENT 0 IS 1ELEMENT 1 IS 2ELEMENT 2 IS 3ELEMENT 3 IS 4ELEMENT 4 IS 5ELEMENT 5 IS 6ELEMENT 6 IS 7ELEMENT 7 IS 8ELEMENT 8 IS 9ELEMENT 9 IS 10iscopy is true,copy back the content and free the elems buffer234567891011请按任意键继续. . .*/
// Sample2.cpp : 定义 DLL 应用程序的导出函数。//#include "stdafx.h"#include "..\Sample2.h"JNIEXPORT jintArray JNICALL Java_Sample2_intMethod (JNIEnv *env, jobject obj){ jint i = 1; jintArray array;//定义数组对象 array = env-> NewIntArray(10); for(; i<= 10; i++) env->SetIntArrayRegion( array, i-1, 1, &i); /* 获取数组对象的元素个数 */ int len = env->GetArrayLength( array); /* 获取数组中的所有元素 */ jboolean iscopy; //jint* elems = env-> GetIntArrayElements(array, &iscopy); jint* elems = (jint*)env-> GetPrimitiveArrayCritical(array, &iscopy); if(iscopy) printf("iscopy is true\n"); else printf("iscopy is false\n"); for(i=0; i<len; i++) printf("ELEMENT %d IS %d\n", i, elems[i]); for (int i=0; i<len; i++) elems[i]+=1; env->ReleasePrimitiveArrayCritical(array,elems,0); //env->ReleaseIntArrayElements(array,elems,0);//释放~~~ // 对于最后一个参数(如果指针指向的数组为副本时,否则该参数不起作用) // 0 copy back the content and free the elems buffer // JNI_COMMIT copy back the content but do not free the elems buffer // JNI_ABORT free the buffer without copying back the possible changes if(iscopy) printf("iscopy is true,copy back the content and free the elems buffer\n"); else printf("iscopy is false,理论上副本elems的改变直接导致array的改变!\n");return array;}