简体   繁体   English

Java未选中:为varargs参数创建未经检查的通用数组

[英]Java unchecked: unchecked generic array creation for varargs parameter

I have set Netbeans to show unchecked warnings in my Java code, but I am failing to understand the error on the following lines: 我已将Netbeans设置为在我的Java代码中显示未经检查的警告,但我无法理解以下行中的错误:

private List<String> cocNumbers;
private List<String> vatNumbers;
private List<String> ibans;
private List<String> banks;
...
List<List<String>> combinations = Utils.createCombinations(cocNumbers, vatNumbers, ibans);

Gives: 得到:

[unchecked] unchecked generic array creation for varargs parameter of type List<String>[]

Method source: 方法来源:

/**
 * Returns a list of all possible combinations of the entered array of lists.
 *
 * Example: [["A", "B"], ["0", "1", "2"]]
 * Returns: [["A", "0"], ["A", "1"], ["A", "2"], ["B", "0"], ["B", "1"], ["B", "2"]]
 *
 * @param <T> The type parameter
 * @param elements An array of lists
 * @return All possible combinations of the entered lists
 */
public static <T> List<List<T>> createCombinations(List<T>... elements) {
    List<List<T>> returnLists = new ArrayList<>();

    int[] indices = new int[elements.length];
    for (int i = 0; i < indices.length; i++) {
        indices[i] = 0;
    }

    returnLists.add(generateCombination(indices, elements));
    while (returnLists.size() < countCombinations(elements)) {
        gotoNextIndex(indices, elements);
        returnLists.add(generateCombination(indices, elements));
    }

    return returnLists;
}

What is exactly going wrong and how would I fix it, as I suppose leaving unchecked warnings in the code is not a good idea? 究竟出了什么问题以及如何修复它,因为我想在代码中留下未经检查的警告并不是一个好主意?

Forgot to mention, but I am using Java 7. 忘了提,但我使用的是Java 7。

Edit : Also I see now that the method has the following: 编辑 :我现在也看到该方法具有以下内容:

[unchecked] Possible heap pollution from parameterized vararg type List<T>
  where T is a type-variable:
    T extends Object declared in method <T>createCombinations(List<T>...)

As janoh.janoh mentioned above, varargs in Java is just a syntactic sugar for arrays plus the implicit creation of an array at the calling site. 正如上面提到的janoh.janoh,Java中的varargs只是数组的语法糖,加上在调用站点隐式创建数组。 So 所以

List<List<String>> combinations =
    Utils.createCombinations(cocNumbers, vatNumbers, ibans);

is actually 实际上是

List<List<String>> combinations =
    Utils.createCombinations(new List<String>[]{cocNumbers, vatNumbers, ibans});

But as you may know, new List<String>[] is not allowed in Java, for reasons that have been covered in many other questions, but mainly have to do with the fact that arrays know their component type at runtime, and check at runtime whether elements added match its component type, but this check is not possible for parameterized types. 但正如您所知,Java中不允许使用new List<String>[] ,原因已在许多其他问题中得到解决,但主要与数组在运行时知道其组件类型的事实有关,并在运行时是否添加的元素与其组件类型匹配,但对于参数化类型不能进行此检查。

Anyway, rather than failing, the compiler still creates the array. 无论如何,编译器仍然创建数组,而不是失败。 It does something similar to this: 它做了类似的事情:

List<List<String>> combinations =
    Utils.createCombinations((List<String>[])new List<?>[]{cocNumbers, vatNumbers, ibans});

This is potentially unsafe, but not necessarily unsafe. 这可能不安全,但不一定不安全。 Most varargs methods simply iterate over the varargs elements and read them. 大多数varargs方法只是迭代varargs元素并读取它们。 In this case, it doesn't care about the runtime type of the array. 在这种情况下,它不关心数组的运行时类型。 This is the case with your method. 您的方法就是这种情况。 Since you are on Java 7, you should add the @SafeVarargs annotation to your method, and you won't get this warning anymore. 由于您使用的是Java 7,因此您应该将@SafeVarargs注释添加到您的方法中,您将不再收到此警告。 This annotation basically says, this method only cares about the types of the elements, not the type of the array. 这个注释基本上说,这个方法只关心元素的类型,而不是数组的类型。

However, there are some varargs methods that do use the runtime type of the array. 但是,有一些varargs方法确实使用了数组的运行时类型。 In this case, it is potentially unsafe. 在这种情况下,它可能是不安全的。 That's why the warning is there. 这就是警告的原因。

Because java compiler uses an implicit array creation for varargs, and java doesn't allow a generic array creation (because type argument is not reifiable). 因为java编译器为varargs使用隐式数组创建,并且java不允许创建泛型数组(因为类型参数不可再生)。

The code below is correct (these operations are allowed with arrays), so unchecked warning is needed: 下面的代码是正确的(数组允许这些操作),因此需要取消选中警告:

public static <T> List<List<T>> createCombinations(List<T> ... lists) {
    ((Object[]) lists)[0] = new ArrayList<Integer>();
    // place your code here
}

See a comprehensive explanation here 在此处查看详细说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “为Matcher类型的varargs参数创建未经检查的通用数组 <? extends String> []“使用CoreMatchers.allOf()发出警告 - “Unchecked generic array creation for varargs parameter of type Matcher <? extends String> []” warning using CoreMatchers.allOf() IntelliJ警告:为varargs参数创建未选中的泛型数组 - IntelliJ warning: Unchecked generics array creation for varargs parameter Android AsyncTask,警告:“为varargs参数创建未经检查的泛型数组” - Android AsyncTask, warning: “Unchecked generics array creation for varargs parameter” 如何在 JavaFX 的 XYChart 中删除“为可变参数创建未经检查的泛型数组”? - How to remove "unchecked generics array creation for varargs" in XYChart on JavaFX? Java泛型:数组创建,类型转换和未经检查的警告 - Java generics: array creation, typecasting and unchecked warnings Java 通用:未经检查的演员表 - Java generic: Unchecked cast Java 1.7 varargs函数报告为未经检查的警告 - Java 1.7 varargs function reported as unchecked warning Java通用继承“ unchecked cast” - Java generic inheritance “unchecked cast” Java未经检查的操作转换为泛型 - Java unchecked operation cast to generic 未经检查的泛型抽象继承(java) - Unchecked generic in abstract inheritance (java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM