2015-10-07 41 views
0

我有返回collection<"Integer"> Java方法,我想在河用它转换Java集合 类型与R对象的Java方法是 使用JNI

public Collection<Integer> search(){ ....} 

使用JNI,应该是什么类型R中的这个(Java集合)对象。我尝试了"[I",这意味着一个整数数组,但它没有奏效。

+1

据我所知,当你处理与JNI代码Java泛型你需要使用原始类型,即'Ljava/util/Collection;' – Michael

+0

我试过[Ljava/util/Collection;但仍然没有运气。 –

+0

'Ljava/util/Collection;',而不是'[Ljava/util/Collection;'。这不是一个“集合”的数组。 – Michael

回答

1

Collection<Integer>是通过参数化通用接口Collection创建的类型,它允许在使用Collection<Integer>时执行一些编译时健全性检查。

但是,在运行时,剩下的Collection<Integer>只是the raw typeCollection。因此,如果您尝试使用FindClass来找到合适的课程,则应查找java.util.Collection,即"java/util/Collection"

一旦你对类的引用,并以该类你可以用CollectiontoArray方法来获取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); 
+0

您能否指定更多关于toArray的内容。我应该在哪里使用它?在'公共集合 search(){....}'或某处在JNI规范类型中进行收集。一个小例子会有帮助 –

+0

@HaroonRashid:我已经添加了一个代码示例。我还修正了一个错字(应该传递给'FindClass'的字符串是''java/util/Collection'',而不是''Ljava/util/Collection;'') – Michael