复制C中的数组到Java数组使用JNI [英] Copying C Array into Java Array Using JNI

查看:374
本文介绍了复制C中的数组到Java数组使用JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用C无符号整数数组和多头的Java数组。我想的无符号整数的内容复制到Java数组。到目前为止,我发现这样做的唯一功能是SetLongArrayRegion(),但是这需要整个缓冲区数组。有没有设置只有Java数组中的单个元素的函数?

I have an array of unsigned integers in C and a java array of longs. I want to copy the contents of the unsigned integers to the java array. So far, the only function that I've found to do this is SetLongArrayRegion(), but this takes an entire buffer array. Is there a function to set only the individual elements of the java array?

推荐答案

有也是原始的长类型来设置JNI各个元素的功能。所以,我相信你想拥有的是这样的事情

There is also a function for the primitive 'long' type to set individual elements in JNI. So I believe what you want to have is something like this

unsigned int* cIntegers = getFromSomewhere();
int elements = sizeof(cIntegers) / sizeof(int);

jfieldID jLongArrayId = env->GetFieldID(javaClass, "longArray", "[J");
jlongArray jLongArray = (jlongArray) env->GetObjectField(javaObject, jLongArrayId);
for (unsigned int i = 0; i < elements; ++i) {
   unsigned int cInteger = cIntegers[i];
   long cLong = doSomehowConvert(cInteger);
   env->SetLongArrayElement(jLongArray, i, (jlong) cLong);
}

如果在Java中的多头排列被称为 longArray 和Java类保存在一个JNI JCLASS变量 JavaClass中

if the long array in java is called longArray and the java class is saved in a JNI jclass variable javaClass.

这篇关于复制C中的数组到Java数组使用JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆