问题
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);
}
}
回答1:
This is a raw type:
ArrayList blocksToSkip
Java expects everything, not only the Block
type.
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>();
来源:https://stackoverflow.com/questions/27325278/what-is-type-mismatch-and-how-do-i-fix-it