JNI~可變數組

/****************************************************************************
 *  Java declaration:
 *     native public void Pass2DByteArray(byte[][] array);
 *
 *  Pass a 2-D array of bytes from Java. This method will retrieve each
 *  element from the arrays, print it, then multiply it by 10 and store
 *  the new value back into the array. This is to show access/updating
 *  of 2-D arrays within native code. The process would be similar for
 *  3-D arrays. New 12-18-98
 *
*/
JNIEXPORT void JNICALL Java_Native_pass2DByteArray(JNIEnv *env, jobject obj, 
        jobjectArray array2D) {
   // Get length of the array (how many arrays?)
   unsigned int num_arrays = env->GetArrayLength(array2D);
   printf("\nRunning pass2DByteArray (%d)\n",num_arrays);
   
   printf("In native method printing 2-D byte array.\n");
   printf("Each element is then multiplied by 10 and updated.\n");
   
   // Loop over each array
   for (int i = 0; i < num_arrays; i++)	{
      // Get the object at the i'th position (it's an array of bytes)
      jbyteArray array = (jbyteArray) env->GetObjectArrayElement(array2D, i);
      
      // Get the length of this array of bytes
      unsigned int length = env->GetArrayLength(array);

      // Get the elements from the byte array
      jbyte *elements = env->GetByteArrayElements(array, 0);
		
      // Print each element, then multiply the value by 10
      // and put it back into the array. (This is to prove that
      // the array elements have changed when this function returns
      // to Java code.
      for (int j = 0; j < length; j++) {
	 printf("%i,%i = %i\n", i, j, elements[j]);
	 
	 // Update the element (just multiply it by 10)
	 elements[j] = elements[j] * 10;
      }
   }
}

 

參考:

http://gauss.ececs.uc.edu/Courses/c694/lectures/JNI_2/native.cc

https://swarm.workshop.perforce.com/files/guest/michael_bishop/P4APIForJava/c-cpp/JNIStringUtil.c

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章