In JNI, it is relatively simple to access data of integer, floating point, and complex types in Java classes. For example:
// Obtain the class name jclass CLS = (* env)-> getobjectclass (ENV, OBJ); // obtain the variable numberjfieldid fnumber = (* env) in the Java class) -> getstaticfieldid (ENV, CLS, "Number", "I"); // set the value of njninumber in JNI that is also in C code to fnumber, in fact, the value is set to number (* env)-> setstaticintfield (ENV, CLS, fnumber, njninumber) in the Java class );
However, the use of one-dimensional arrays and two-dimensional arrays in JNI is somewhat different from the above, and it is also a little troublesome. Let's take a look at how to use it.
A piece of code found on the Internet shows the use of two-dimensional arrays in JNI. I think it is good. I also sorted out the Code as follows:
Java_com_array_arraytest_arrayuse (jnienv * ENV, jobject thiz, jobjectarray arraydata) {jint I, j; int ROW = (* env)-> getarraylength (ENV, arraydata ); // obtain the number of rows jarray myarray = (* env)-> getobjectarrayelement (ENV, arraydata, 0); int Col = (* env)-> getarraylength (ENV, myarray ); // obtain the number of columns jint jnidata [row] [col]; for (I = 0; I <row; I ++) {myarray = (* env) -> getobjectarrayelement (ENV, arraydata, I); jint * coldata = (* env)-> getintarrayelements (ENV, (jintarray) myarray, 0); For (j = 0; j <Col; j ++) {jnidata [I] [J] = coldata [J]; // retrieves arraydata data in the Java class, and assigned to the array} (* env) in JNI-> releaseintarrayelements (ENV, (jintarray) myarray, coldata, 0 );}}
Arraydata passes a two-dimensional array int [] [] testarray = new int [50] [50] defined in Java through the parameter;
1. In the above example, we only get the data in the array. How can we set the data in the array? Let's take a look at the following code:
Java_com_array_arraytest_arrayuse (jnienv * ENV, jobject thiz, jobjectarray arraydata) {Jin I, J; jint COUNT = 10; jint jnidata [10] [10]; for (I = 0; I <count; I ++) {for (j = 0; I <count; j ++ {jnidata [I] [J] = I * (I + J) ;}} for (I = 0; I <row; I ++) {// get the first address of the array, jarray myarray = (* env)-> getobjectarrayelement (ENV, arraydata, I )); // copy the Count elements of the JNI array to the myarray array (* env)-> setintarrayregion (ENV, myarray, 0, Count, jnidata [I]); (* env) -> releaseintarrayelements (ENV, (jintarray) myarray, coldata, 0 );}}
If arraydata is a one-dimensional array, remove the for loop above.
2. How to set data in a one-dimensional string array? Let's look at the following code:
Values (jnienv * ENV, jobject thiz, jobjectarray arraystringdata) {Jin I; char temp [100]; jint COUNT = (* env)-> getarraylength (ENV, arraystringdata ); for (I = 0; I <count; I ++) {memset (temp, 0,100); sprintf (temp, "str: % d", I ); // obtain the jstring data jstring STR = (* env)-> newstringutf (ENV, temp); // set STR to the I element of arraystringdata. (* Env)-> setobjectarrayelement (ENV, arraystringdata, I, STR );}}
Arraystringdata passes a one-dimensional array string [] testarray = new string [50] defined in Java;
3. How to Set Data for Two-Dimensional String data? See the following code:
Values (jnienv * ENV, jobject thiz, jobjectarray arraystringdata) {Jin I, J; char temp [100]; jint COUNT = (* env)-> getarraylength (ENV, arraystringdata ); for (I = 0; I <count; I ++) {// get a row of Data jarray myarray = (* env)-> getobjectarrayelement (ENV, arraystringdata, i); jint count1 = (* env)-> getarraylength (ENV, myarray); For (j = 0; j <count1; j ++) {memset (temp, 0,100); sprintf (TEM P, "str: % d", I * (I + J); // get the jstring data jstring STR = (* env)-> newstringutf (ENV, temp); // set STR to the J element of arraystringdata. (* Env)-> setobjectarrayelement (ENV, myarray, J, STR );}}}
Arraystringdata passes a two-dimensional array string [] [] testarray = new string [50] [50] defined in Java using parameters.
4. The methods used to access the array above are implemented by passing parameters. How can I directly obtain an array without passing parameters in JNI for operations?
I searched for related materials and articles on the Internet and introduced them in this regard. When I was using their methods, the program always went wrong and I did not know why. Therefore, so far, I still haven't figured out how to directly access arrays in Java without passing parameters.
I posted a post on the Forum and did not find the corresponding method. The post on the forum is as follows:
Http://topic.csdn.net/u/20120315/10/f956498c-ed93-4d4f-bfb2-cc1719de61d7.html
If you have any good comments, you can send me a private message or leave a message. Thank you.