Java 原因:推断类型不符合上限
我试图寻找类似的答案,但我还没有找到解决办法,于是我直接问,并揭露了我的情况 我有一个静态函数Java 原因:推断类型不符合上限,java,arrays,generics,java-8,Java,Arrays,Generics,Java 8,我试图寻找类似的答案,但我还没有找到解决办法,于是我直接问,并揭露了我的情况 我有一个静态函数validate private static void validate(AiNode pNode) { ... for (AiNode child : pNode.mChildren) { doValidation(child.mMeshes, child.mMeshes.length, "a", "b");
validate
private static void validate(AiNode pNode) {
...
for (AiNode child : pNode.mChildren) {
doValidation(child.mMeshes, child.mMeshes.length, "a", "b");
}
}
}
pNode.mChildren
是AiNode
的数组
这是我的doValidation
private static <T> void doValidation(T[] pArray, int size, String firstName, String secondName) {
// validate all entries
if (size > 0) {
if (pArray == null) {
throw new Error("aiScene." + firstName + " is NULL (aiScene." + secondName + " is " + size + ")");
}
for (int i = 0; i < size; i++) {
if (pArray[i] != null) {
validate(parray[i]);
}
}
}
}
private static void doValidation(T[]pArray,int size,String firstName,String secondName){
//验证所有条目
如果(大小>0){
if(pArray==null){
抛出新错误(“aiScene.+firstName+”为空(aiScene.+secondName+”为“+size+”);
}
对于(int i=0;i
但我总是犯这个错误
method doValidation in class ValidateDataStructure cannot be applied to given types;
required: T[],int,String,String
found: int[],int,String,String
reason: inferred type does not conform to upper bound(s)
inferred: int
upper bound(s): Object
where T is a type-variable:
T extends Object declared in method <T>doValidation(T[],int,String,String)
----
(Alt-Enter shows hints)
类ValidateDataStructure中的方法doValidation不能应用于给定类型;
必需:T[],int,String,String
找到:int[],int,String,String
原因:推断类型不符合上限
推断:int
上限:对象
其中T是一个类型变量:
T扩展方法doValidation中声明的对象(T[],int,String,String)
----
(Alt-Enter显示提示)
我认为这与java中原语类型的数组扩展了Object[]这一事实有关,事实上,如果我将T[]
切换到T
它就可以工作了,但是我不能再循环它了。。。我不知道如何解决这个问题,也不知道哪种解决方案最适合我的情况
其思想是根据数组
T
类型使用不同的validate(T[]array)
,您的数组是int[]
,而不是Integer[]
。要将其转换为整数[]
使用
for (AiNode child : pNode.mChildren) {
Integer[] meshes = Arrays.stream(child.mMeshes).boxed().toArray(Integer::new);
doValidation(meshes, child.mMeshes.length, "a", "b");
}
信息说明了一切。
int
不能是T
。使用整数[]代替int[],因为这里不需要T
;您只需声明它私有静态void-doValidation(Object[]pArray,int-size,String-firstName,String-secondName)
哦,伙计,您说得对。。!谢谢