ele*_*ect 6 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
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]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我不断收到此错误
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)
Run Code Online (Sandbox Code Playgroud)
我认为这与java中的原始类型数组扩展了Object []有关,确实如果我切换T[]到T它可以工作,但是我不能再循环它了......我不知道如何解决它或哪个解决方案最适合我的情况。
这个想法是validate(T[] array)基于数组T类型有不同的
你的数组是int[],不是Integer[]。将其转换为Integer[]使用
for (AiNode child : pNode.mChildren) {
Integer[] meshes = Arrays.stream(child.mMeshes).boxed().toArray(Integer::new);
doValidation(meshes, child.mMeshes.length, "a", "b");
}
Run Code Online (Sandbox Code Playgroud)