15.2.2.4 传递整型数组

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;

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值