-3
此代碼不起作用;無法將枚舉數組添加到Collection
ArrayList<BlockFace> cardinalDirections = new ArrayList<>();
cardinalDirections.addAll(new BlockFace[] {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST});
這也不是;
ArrayList<BlockFace> cardinalDirections = Arrays.asList(new BlockFace[] {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST});
它只工作,如果我手動添加每個元素一次,或迭代通過數組;
ArrayList<BlockFace> cardinalDirections = new ArrayList<>();
for (BlockFace face : new BlockFace[] {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST})
cardinalDirections.add(face);
爲什麼前兩個例子不起作用?
'列表 cardinalDirections = Arrays.asList(新BlockFace [] {BlockFace.NORTH,BlockFace.SOUTH,BlockFace.EAST,BlockFace.WEST});'代替' ArrayList ' –
Tunaki
'addAll'沒有接受數組的重載。 – Savior
而'Arrays.asList'有一個返回類型'List'。 – Savior