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 true
ELEMENT 0 IS 1
ELEMENT 1 IS 2
ELEMENT 2 IS 3
ELEMENT 3 IS 4
ELEMENT 4 IS 5
ELEMENT 5 IS 6
ELEMENT 6 IS 7
ELEMENT 7 IS 8
ELEMENT 8 IS 9
ELEMENT 9 IS 10
1
2
3
4
5
6
7
8
9
10
请按任意键继续. . .
*/
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 true
ELEMENT 0 IS 1
ELEMENT 1 IS 2
ELEMENT 2 IS 3
ELEMENT 3 IS 4
ELEMENT 4 IS 5
ELEMENT 5 IS 6
ELEMENT 6 IS 7
ELEMENT 7 IS 8
ELEMENT 8 IS 9
ELEMENT 9 IS 10
iscopy is true,copy back the content and free the elems buffer
2
3
4
5
6
7
8
9
10
11
请按任意键继续. . .
*/
// 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;
}