選中的Java方法調用運行我的代碼,我得到這樣的警告:與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()'的返回值從'List'更改爲'List'。 –
Turamarth