Java 未经检查的 ArrayList 方法调用
Java unchecked method invocation with ArrayList
当我 运行 我的代码时,我收到此警告:
warning: [unchecked] unchecked method invocation: method addAll in interface List is applied to given types
snakeDotlist.addAll(genFirstDots());
required: Collection<? extends E>
found: List
where E is a type-variable:
E extends Object declared in interface List
代码:
initDisplay();
List<Sprite> snakeDotlist = new ArrayList<>();
snakeDotlist.addAll(genFirstDots());
Sprite tokenSprite = new Sprite((genRandomNumber(0, 64)), (genRandomNumber(0, 48)), 16, 16, "res/snakedot.png");
while(!Display.isCloseRequested())
{
if (hasCollided(tokenSprite, snakeDotlist.get(0)))
{
tokenSprite.updateToken(true);
snakeDotlist.get(0).score += 1;
snakeDotlist.addAll(genNewDots((int)snakeDotlist.get(0).prev1x, (int)snakeDotlist.get(0).prev1y, (int)snakeDotlist.get(0).prev2x, (int)snakeDotlist.get(0).prev1y));
}
.
public static List genFirstDots()
{
List<Sprite> list = new ArrayList<>();
list.add(new Sprite(3, 0, 16, 16, "res/snakedot.png"));
list.add(new Sprite(2, 0, 16, 16, "res/snakedot.png"));
list.add(new Sprite(1, 0, 16, 16, "res/snakedot.png"));
list.add(new Sprite(0, 0, 16, 16, "res/snakedot.png"));
return list;
}
我看了一些其他的帖子,说要从
更改 snakeDotList 的声明
List<Sprite> snakeDotlist = new ArrayList<Sprite>();
到
List<Sprite> snakeDotlist = new ArrayList<>();
但我仍然收到警告消息,我不知道如何解决,有帮助吗?
您(只)需要将 genFirstDots()
的声明从
更改为
public static List genFirstDots()
到
public static List<Sprite> genFirstDots()
编译器告诉您它需要一个类型化集合,但只找到了一个泛型 List
。
旁注:
List<Sprite> snakeDotlist = new ArrayList<>();
这就是所谓的 diamond operator,它是在 Java 7 中引入的,它允许您跳过 Sprite
的重复声明。它确实改变了 snakeDotlist
将是 List<Sprite>
.
的事实
你应该改变这个:
public static List genFirstDots()
对此:
public static List<Sprite> genFirstDots()
出现警告的原因是因为您在 genFirstDots()
中返回 List
,但您将返回值添加到 List<Sprite>
。
当没有给出泛型类型参数时,泛型类型隐含地有一个 Object
类型参数,所以 List
实际上是 List<Object>
。当您尝试将对象列表附加到 sprite 列表时,对象列表存储的对象可能与 Sprite
不兼容并且无法添加到 sprite 列表。因此警告。
当我 运行 我的代码时,我收到此警告:
warning: [unchecked] unchecked method invocation: method addAll in interface List is applied to given types
snakeDotlist.addAll(genFirstDots());
required: Collection<? extends E>
found: List
where E is a type-variable:
E extends Object declared in interface List
代码:
initDisplay();
List<Sprite> snakeDotlist = new ArrayList<>();
snakeDotlist.addAll(genFirstDots());
Sprite tokenSprite = new Sprite((genRandomNumber(0, 64)), (genRandomNumber(0, 48)), 16, 16, "res/snakedot.png");
while(!Display.isCloseRequested())
{
if (hasCollided(tokenSprite, snakeDotlist.get(0)))
{
tokenSprite.updateToken(true);
snakeDotlist.get(0).score += 1;
snakeDotlist.addAll(genNewDots((int)snakeDotlist.get(0).prev1x, (int)snakeDotlist.get(0).prev1y, (int)snakeDotlist.get(0).prev2x, (int)snakeDotlist.get(0).prev1y));
}
.
public static List genFirstDots()
{
List<Sprite> list = new ArrayList<>();
list.add(new Sprite(3, 0, 16, 16, "res/snakedot.png"));
list.add(new Sprite(2, 0, 16, 16, "res/snakedot.png"));
list.add(new Sprite(1, 0, 16, 16, "res/snakedot.png"));
list.add(new Sprite(0, 0, 16, 16, "res/snakedot.png"));
return list;
}
我看了一些其他的帖子,说要从
更改 snakeDotList 的声明List<Sprite> snakeDotlist = new ArrayList<Sprite>();
到
List<Sprite> snakeDotlist = new ArrayList<>();
但我仍然收到警告消息,我不知道如何解决,有帮助吗?
您(只)需要将 genFirstDots()
的声明从
public static List genFirstDots()
到
public static List<Sprite> genFirstDots()
编译器告诉您它需要一个类型化集合,但只找到了一个泛型 List
。
旁注:
List<Sprite> snakeDotlist = new ArrayList<>();
这就是所谓的 diamond operator,它是在 Java 7 中引入的,它允许您跳过 Sprite
的重复声明。它确实改变了 snakeDotlist
将是 List<Sprite>
.
你应该改变这个:
public static List genFirstDots()
对此:
public static List<Sprite> genFirstDots()
出现警告的原因是因为您在 genFirstDots()
中返回 List
,但您将返回值添加到 List<Sprite>
。
当没有给出泛型类型参数时,泛型类型隐含地有一个 Object
类型参数,所以 List
实际上是 List<Object>
。当您尝试将对象列表附加到 sprite 列表时,对象列表存储的对象可能与 Sprite
不兼容并且无法添加到 sprite 列表。因此警告。