将int更改为整数

时间:2016-06-14 08:51:12

标签: java integer int field

我有方法electCouncillor从某个数组CouncillorsSet获取某个议员,将其放在某个balcony队列上,从balcony获取额外的议员把它放回CouncillorSet,以确定哪个议员将从数组中取出我有私有int字段CouncillorNumber,然后我意识到我不能使用int因为每次我使用那种方法我有重置CouncillorNumber值并且int不允许null(如果我输入0它将始终使用数组中的第一个councilor),所以我决定将其更改为整数但由于某种原因测试失败并且我无法弄清楚原因。

public class Player {
    private int id;
    private int councillorNumber;

    public void electCouncillor(Region region,GameBoard gameBoard){
//Gets a certain councillor from the array CouncillorSet
        Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber);
//Adds that councillor to the queue Balcony
        region.getBalcony().add(councillor);
//Sets the councillor on top of the Balcony in the CouncillorSet
        gameBoard.setCouncillor(region.getBalcony().element());
//Removes the councillor on top of the balcony
        region.getBalcony().remove();
//Removes the councillor from CouncillorSet that was added to the balcony
        gameBoard.getCouncillorsSet().remove(councillorNumber);


    }
    public int getCouncillorNumber() {
        return councillorNumber;
    }
    public void setCouncillorNumber(int councillorNumber) {
        this.councillorNumber = councillorNumber;
    }

}

这是测试

@Test
    public void testElectCouncillor(){
        Player player = new Player(1);
        GameBoard gameBoard = new GameBoard();
//First i create 6 councillors to be used in the test
        Councillor councillor1 = new Councillor();
        Councillor councillor2 = new Councillor();
        Councillor councillor3 = new Councillor();
        Councillor councillor4 = new Councillor();
        Councillor councillor5 = new Councillor();
        Councillor councillor6 = new Councillor();
        councillor1.setColor(Color.BLACK);
        councillor2.setColor(Color.BLUE);
        councillor3.setColor(Color.ORANGE);
        councillor4.setColor(Color.PURPLE);
        councillor5.setColor(Color.WHITE);
        councillor6.setColor(Color.PINK);
//Then i add the first 4 councillors to a balcony
        Region region = gameBoard.getRegionCoast();
        Queue<Councillor> balcony = region.getBalcony();
        balcony.add(councillor1);
        balcony.add(councillor2);
        balcony.add(councillor3);
        balcony.add(councillor4);
//The i add the remaining two to the CouncillorSet of the GameBoard
        gameBoard.setCouncillorSet(new ArrayList<Councillor>());
        gameBoard.getCouncillorsSet().add(councillor5);
        gameBoard.getCouncillorsSet().add(councillor6);
//This gets the first element from CouncillorSet, which in this case is the councillor5 color white
        player.setCouncillorNumber(0);
        player.electCouncillor(region,gameBoard);
//After doing the method first i verify that the element in top of the queue is 
//now the councillor Blue
        assertEquals(Color.BLUE,region.getBalcony().element().getColor());
//Then i verify that the elements 0 and 1 from the CouncillorSet are the
//Pink and Black councillors respectively (before the method the pink
//councillor was the element 1)
        assertEquals(Color.BLACK,gameBoard.getCouncillorsSet().get(1).getColor());
        assertEquals(Color.PINK,gameBoard.getCouncillorsSet().get(0).getColor());
}

这是使用整数而不是int

的方法的版本
public class Player {
    private int id;
    private Integer councillorNumber;

    public void electCouncillor(Region region,GameBoard gameBoard){
        Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber);
        region.getBalcony().add(councillor);
        gameBoard.setCouncillor(region.getBalcony().element());
        region.getBalcony().remove();
        gameBoard.getCouncillorsSet().remove(councillorNumber);


    }
    public Integer getCouncillorNumber() {
        return councillorNumber;
    }
    public void setCouncillorNumber(Integer councillorNumber) {
        this.councillorNumber = councillorNumber;
    }

}

运行相同测试时出现的错误是expected <BLACK>, but was <PINK>

1 个答案:

答案 0 :(得分:2)

取自API,根据参数类型有不同的方法。第一个删除指示的索引中的元素,而第二个删除元素本身,无论索引

  

remove(int index)   删除

中指定位置的元素      

remove(Object o)删除第一次出现的   此列表中的指定元素(如果存在)。

编辑添加示例:

列表= {1,4,5,6,9}

使用int =&gt;删除(4)(删除索引为4的数字) 结果:List = {1,4,5,6}

使用Integer =&gt; remove(4)(删除值为4的元素) 结果:列表= {1,5,6,9}