之前的文章讲解了有关JNI使用方法,这篇文章进阶一点,介绍下JNI操作二维数组的方法。有了之前文章的操作JNI的方法,这里直接上代码了。
Java代码部分
1 package com.testjni; 2 3 public class TestJNI 4 { 5 private static final int ONE_DIMENSION = 3; 6 7 private static final int TWO_DIMENSION = 2; 8 9 public native int test2DimensionArray(int[][] aHoleList); 10 11 static 12 { 13 System.load("D:\\eclipseWebWorkSpace\\testest\\src\\com\\testjni\\libTestJNI_amd64.dll"); 14 } 15 16 public static void main(String[] args) 17 { 18 TestJNI testJNI = new TestJNI(); 19 20 //测试二维数组 21 int[][] test = new int[ONE_DIMENSION][TWO_DIMENSION]; 22 System.out.println("\njava before--->"); 23 for (int i = 0; i < ONE_DIMENSION; i++) 24 { 25 StringBuilder sb = new StringBuilder("test["); 26 for (int j = 0; j < TWO_DIMENSION; j++) 27 { 28 if (j == 0) 29 { 30 sb.append(i).append("] = ").append(test[i][j]).append(" "); 31 } 32 else 33 { 34 sb.append(test[i][j]).append(" "); 35 } 36 } 37 System.out.println(sb.toString()); 38 } 39 int res = testJNI.test2DimensionArray(test); 40 41 System.out.println("\njava after--->res=" + res); 42 for (int i = 0; i < ONE_DIMENSION; i++) 43 { 44 StringBuilder sb = new StringBuilder("test["); 45 for (int j = 0; j < TWO_DIMENSION; j++) 46 { 47 if (j == 0) 48 { 49 sb.append(i).append("] = ").append(test[i][j]).append(" "); 50 } 51 else 52 { 53 sb.append(test[i][j]).append(" "); 54 } 55 } 56 System.out.println(sb.toString()); 57 } 58 } 59 }
C头文件
1 /* DO NOT EDIT THIS FILE - it is machine generated */ 2 #include "jni.h" 3 /* Header for class com_testjni_TestJNI */ 4 5 #ifndef _Included_com_testjni_TestJNI 6 #define _Included_com_testjni_TestJNI 7 #ifdef __cplusplus 8 extern "C" { 9 #endif 10 /* 11 * Class: com_testjni_TestJNI 12 * Method: nativeBsmGetHoleList 13 * Signature: ([[I)I 14 */ 15 JNIEXPORT jint JNICALL Java_com_testjni_TestJNI_test2DimensionArray 16 (JNIEnv *, jobject, jobjectArray); 17 18 #ifdef __cplusplus 19 } 20 #endif 21 #endif
C源码文件
1 #include <stdio.h> 2 #include "com_testjni_TestJNI.h" 3 4 #define ONE_DIMENSION 3 5 #define TWO_DIMENSION 2 6 JNIEXPORT jint JNICALL Java_com_testjni_TestJNI_test2DimensionArray(JNIEnv *env, jobject obj, jobjectArray test) 7 { 8 int jniTest[ONE_DIMENSION][TWO_DIMENSION]; 9 for (int i = 0; i < ONE_DIMENSION; i++) 10 { 11 for (int j = 0; j < TWO_DIMENSION; j++) 12 { 13 jniTest[i][j] = 10 * (i + 1)+ j; 14 } 15 } 16 17 printf("\njni:\n"); 18 jintArray testArray; 19 for (int i = 0; i < ONE_DIMENSION; i ++) 20 { 21 //从输入参数二维数组aHoleList中取出某个一维数组 22 testArray = (jintArray)env->GetObjectArrayElement(test, i); 23 printf("testArray[%d]=%d, %d, %d, testArray[0] addr = %d; testArray[1] addr = %d, testArray[2] addr = %d; testArray addr=%d; sizeof(jintArray)=%d\n", 24 i, testArray[0], testArray[1], testArray[2], &testArray[0], &testArray[1], &testArray[2], testArray, sizeof(jintArray)); 25 //获取一维数组的指针 26 jint *testP = env->GetIntArrayElements(testArray, NULL); 27 printf("C before, testP[%d]=%d, %d, testP addr=%d\n", i, testP[0], testP[1], testP); 28 //改变数组的值 29 for (int j = 0; j < TWO_DIMENSION; j++) 30 { 31 *(testP + j) = jniTest[i][j]; 32 } 33 printf("C after, testP[%d]=%d, %d, testP addr=%d\n", i, testP[0], testP[1], testP); 34 env->ReleaseIntArrayElements(testArray, testP, 0); 35 printf("\n"); 36 } 37 return 666; 38 }
运行结果
java before---> test[0] = 0 0 test[1] = 0 0 test[2] = 0 0 java after--->res=666 test[0] = 10 11 test[1] = 20 21 test[2] = 30 31 jni: testArray[0]=200, 149, 5485270, testArray[0] addr = 475516656; testArray[1] addr = 475516657, testArray[2] addr = 475516658; testArray addr=475516656; sizeof(jintArray)=8 C before, testP[0]=0, 0, testP addr=38368912 C after, testP[0]=10, 11, testP addr=38368912 testArray[1]=224, 149, 38368982, testArray[0] addr = 475516664; testArray[1] addr = 475516665, testArray[2] addr = 475516666; testArray addr=475516664; sizeof(jintArray)=8 C before, testP[1]=0, 0, testP addr=38368912 C after, testP[1]=20, 21, testP addr=38368912 testArray[2]=248, 149, 38368982, testArray[0] addr = 475516672; testArray[1] addr = 475516673, testArray[2] addr = 475516674; testArray addr=475516672; sizeof(jintArray)=8 C before, testP[2]=0, 0, testP addr=38368912 C after, testP[2]=30, 31, testP addr=38368912
总结:掌握C语言的核心知识点指针,各种操作水到渠成。