gpt4 book ai didi

java - 未经检查的转换警告与对泛型类型的引用

转载 作者:行者123 更新时间:2023-11-29 05:05:31 28 4
gpt4 key购买 nike

您好,我正在研究 Kaplan 自测问题和答案。我真的坚持一种关于未经检查的转换警告的解释。它指出

The following statement will not generate an unchecked conversion warning:

ArrayList list = new ArrayList<>();

但在下一段中,它指出

The following statement will generate unchecked warnings:

ArrayList list = new ArrayList<>(); //generates a conversion warning

我检查了 eclipse 它给出了以下警告消息

ArrayList is a raw type. References to generic type ArrayList should be parameterized

有人可以帮助我吗?这只是一个拼写错误还是我遗漏了什么。提前致谢。

Edit I think i didn't explain my question properly. Actually what i am looking for is "what is the right answer for answering such a question e.g. ArrayList list = new ArrayList<>(); generates unchecked conversion. True/False. " Because in the Kaplan selftest two different statements are given and i am a bit lost, though when i tried the code in Eclipse it doesn't generate Unchecked conversion warning but it gives Reference to generic type.. warning. So i am looking for your suggestion/advice. Thanks once again in advance

最佳答案

对于一个:这是一个警告,如果你小心的话,你可以忽略它,你的代码仍然可以正常编译和运行。

但是,既然你想摆脱它,你应该在定义它时给你的 ArrayList 一个类型。

ArrayList<Type> list = new ArrayList<>();

此类型可以是任何类,但不能是原始类型。如果您需要原语,请使用 Java 提供的包装器类。

关于java - 未经检查的转换警告与对泛型类型的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30527033/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com