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

 mobiledu2502898543 发布于 2023-02-02 18:38

我已将Netbeans设置为在我的Java代码中显示未经检查的警告,但我无法理解以下行中的错误:

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

得到:

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

方法来源:

/**
 * 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  The type parameter
 * @param elements An array of lists
 * @return All possible combinations of the entered lists
 */
public static  List> createCombinations(List... elements) {
    List> 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;
}

究竟出了什么问题以及如何修复它,因为我想在代码中留下未经检查的警告并不是一个好主意?

忘了提,但我使用的是Java 7.

编辑:我现在也看到该方法具有以下内容:

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

newacct.. 148

正如上面提到的janoh.janoh,Java中的varargs只是数组的语法糖,加上在调用站点隐式创建数组.所以

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

实际上是

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

但是您可能知道,new List[]由于许多其他问题已经涵盖的原因,Java中不允许这样做,但主要与数组在运行时知道其组件类型的事实有关,并且在运行时检查添加的元素是否与其组件匹配类型,但参数化类型不能进行此检查.

无论如何,编译器仍然创建数组,而不是失败.它做了类似的事情:

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

这可能不安全,但不一定不安全.大多数varargs方法只是迭代varargs元素并读取它们.在这种情况下,它不关心数组的运行时类型.您的方法就是这种情况.由于您使用的是Java 7,因此应该将@SafeVarargs注释添加到方法中,并且不再会收到此警告.这个注释基本上说,这个方法只关心元素的类型,而不是数组的类型.

但是,有一些varargs方法确实使用了数组的运行时类型.在这种情况下,它可能是不安全的.这就是警告的原因.

2 个回答
  • 因为java编译器为varargs使用隐式数组创建,并且java不允许创建泛型数组(因为类型参数不可再生).

    下面的代码是正确的(数组允许这些操作),因此需要取消选中警告:

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

    请在此处查看详细说明

    2023-02-02 18:42 回答
  • 正如上面提到的janoh.janoh,Java中的varargs只是数组的语法糖,加上在调用站点隐式创建数组.所以

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

    实际上是

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

    但是您可能知道,new List<String>[]由于许多其他问题已经涵盖的原因,Java中不允许这样做,但主要与数组在运行时知道其组件类型的事实有关,并且在运行时检查添加的元素是否与其组件匹配类型,但参数化类型不能进行此检查.

    无论如何,编译器仍然创建数组,而不是失败.它做了类似的事情:

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

    这可能不安全,但不一定不安全.大多数varargs方法只是迭代varargs元素并读取它们.在这种情况下,它不关心数组的运行时类型.您的方法就是这种情况.由于您使用的是Java 7,因此应该将@SafeVarargs注释添加到方法中,并且不再会收到此警告.这个注释基本上说,这个方法只关心元素的类型,而不是数组的类型.

    但是,有一些varargs方法确实使用了数组的运行时类型.在这种情况下,它可能是不安全的.这就是警告的原因.

    2023-02-02 18:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有