使用 JNI 将 Java Collection<Integer> 类型转换为 R 对象
Convert Java Collection<Integer> type to R object using JNI
我有一个 java 方法 returns collection<"Integer">
我想在 R 中使用它。java 方法是
public Collection<Integer> search( ){ ....}
使用 JNI,R 中这个(Java 集合)对象的类型应该是什么。我尝试使用 "[I"
,这意味着一个整数数组,但它没有用。
Collection<Integer>
是通过参数化通用接口 Collection
创建的类型,它允许对 Collection<Integer>
.[=25 的使用执行一些编译时健全性检查=]
但是,在运行时,Collection<Integer>
的剩余部分只是 the raw type Collection
。所以如果你想找到合适的 class 和 FindClass
你应该寻找 java.util.Collection
,即 "java/util/Collection"
.
一旦你有了对 class 的引用和对那个 class 实例的引用,你就可以使用 Collection
中的 toArray
方法来获得一个普通的Integer
个对象的数组,如果这是你想要的。
一个半无意义的小例子(假设你有一个 jobject intColl
指的是你的 Collection<Integer>
):
// Get an array of Objects corresponding to the Collection
jclass collClass = env->FindClass("java/util/Collection");
jmethodID collToArray = env->GetMethodID(collClass, "toArray", "()[Ljava/lang/Object;");
jobjectArray integerArray = (jobjectArray) env->CallObjectMethod(intColl, collToArray);
// Get the first element from the array, and then extract its value as an int
jclass integerClass = env->FindClass("java/lang/Integer");
jmethodID intValue = env->GetMethodID(integerClass, "intValue", "()I");
jobject firstInteger = (jobject) env->GetObjectArrayElement(integerArray, 0);
int i = env->CallIntMethod(firstInteger, intValue);
__android_log_print(ANDROID_LOG_VERBOSE, "Test", "The value of the first Integer is %d", i);
env->DeleteLocalRef(firstInteger);
env->DeleteLocalRef(integerArray);
我有一个 java 方法 returns collection<"Integer">
我想在 R 中使用它。java 方法是
public Collection<Integer> search( ){ ....}
使用 JNI,R 中这个(Java 集合)对象的类型应该是什么。我尝试使用 "[I"
,这意味着一个整数数组,但它没有用。
Collection<Integer>
是通过参数化通用接口 Collection
创建的类型,它允许对 Collection<Integer>
.[=25 的使用执行一些编译时健全性检查=]
但是,在运行时,Collection<Integer>
的剩余部分只是 the raw type Collection
。所以如果你想找到合适的 class 和 FindClass
你应该寻找 java.util.Collection
,即 "java/util/Collection"
.
一旦你有了对 class 的引用和对那个 class 实例的引用,你就可以使用 Collection
中的 toArray
方法来获得一个普通的Integer
个对象的数组,如果这是你想要的。
一个半无意义的小例子(假设你有一个 jobject intColl
指的是你的 Collection<Integer>
):
// Get an array of Objects corresponding to the Collection
jclass collClass = env->FindClass("java/util/Collection");
jmethodID collToArray = env->GetMethodID(collClass, "toArray", "()[Ljava/lang/Object;");
jobjectArray integerArray = (jobjectArray) env->CallObjectMethod(intColl, collToArray);
// Get the first element from the array, and then extract its value as an int
jclass integerClass = env->FindClass("java/lang/Integer");
jmethodID intValue = env->GetMethodID(integerClass, "intValue", "()I");
jobject firstInteger = (jobject) env->GetObjectArrayElement(integerArray, 0);
int i = env->CallIntMethod(firstInteger, intValue);
__android_log_print(ANDROID_LOG_VERBOSE, "Test", "The value of the first Integer is %d", i);
env->DeleteLocalRef(firstInteger);
env->DeleteLocalRef(integerArray);