ArrayRet.java
1 public class ArrayRet{
2 public native int[][] rand2DArray(int size);
3
4 public static void main(String args[]){
5 ArrayRet jniAr=new ArrayRet();
6 System.loadLibrary("randomintdll");
7
8 int randArr[][]=jniAr.rand2DArray(3);
9 for(int i=0;i<3;i++){
10 for(int j=0;j<3;j++){
11 System.out.print(" " + randArr[i][j]);
12 }
13 System.out.println();
14 }
15
16 }
17 }
ArrayRet.h
1 /* DO NOT EDIT THIS FILE - it is machine generated */
2 #include <jni.h>
3 /* Header for class ArrayRet */
4
5 #ifndef _Included_ArrayRet
6 #define _Included_ArrayRet
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 /*
11 * Class: ArrayRet
12 * Method: rand2DArray
13 * Signature: (I)[[I
14 */
15 JNIEXPORT jobjectArray JNICALL Java_ArrayRet_rand2DArray
16 (JNIEnv *, jobject, jint);
17
18 #ifdef __cplusplus
19 }
20 #endif
21 #endif
ArrayRet.cpp
1 #include "ArrayRet.h"
2 #include <time.h>
3 #include <stdlib.h>
4
5 JNIEXPORT jobjectArray JNICALL Java_ArrayRet_rand2DArray
6 (JNIEnv *env, jobject obj, jint size){
7 jobjectArray result;
8 int i;
9 jclass intArrCls=env->FindClass("[I");
10 if(intArrCls==NULL){
11 return NULL;
12 }
13 result = env->NewObjectArray(size,intArrCls,NULL);
14 if(result==NULL){
15 return NULL;
16 }
17 srand(time(NULL));
18
19 for(i=0;i<size;i++){
20 jint tmp[256];
21 int j;
22 jintArray iarr= env->NewIntArray(size);
23 if(iarr==NULL){
24 return NULL;
25 }
26 ;
27 for(j=0;j<size;j++){
28
29 int randVal=rand() % 10;
30 tmp[j]=randVal;
31 }
32 env->SetIntArrayRegion(iarr,0,size,tmp);
33 env->SetObjectArrayElement(result,i,iarr);
34 env->DeleteLocalRef(iarr);
35 }
36 return result;
37 }
Download source codes.
0 comments:
Post a Comment