简体   繁体   English

什么是“类型不匹配”,如何解决?

[英]What is “Type mismatch” and how do I fix it?

How to fix this error? 如何解决这个错误?

Type mismatch: cannot convert from element type Object to Block 类型不匹配:无法从元素类型对象转换为块

I see it at this line: 我在这一行看到它:

for (Block b : blocksToSkip){

Here is the full code. 这是完整的代码。

@EventHandler(priority=EventPriority.NORMAL, ignoreCancelled=true)
public void onEntityExplode(EntityExplodeEvent ev){
    ArrayList blocksToSkip = new ArrayList();
    Location rootLoc = ev.getLocation();
    if (!SkyMagic.IsInIslandWorld(rootLoc)) return;
    for (Block b : ev.blockList()){
        Location loc = b.getLocation();
        IslandData data = SkyMagic.GetIslandAt(loc);
        if ((data != null) && (data.owner != null)){
            blocksToSkip.add(b);
        }
    }
    for (Block b : blocksToSkip){
        ev.blockList().remove(b);
    }
}

This is a raw type : 这是原始类型

ArrayList blocksToSkip

Java expects everything, not only the Block type. Java期望所有东西,而不仅仅是Block类型。 Therefore, you need a type cast . 因此,您需要类型转换

ArrayList blocksToSkip = new ArrayList();

// Rest of your code

for (Object b : blocksToSkip){
    ev.blockList().remove( (Block)b );
}

Note it is discouraged to use raw types. 请注意,建议不要使用原始类型。 You should parameterize instead. 您应该改为参数化

ArrayList<Block> blocksToSkip = new ArrayList<Block>();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM