ArrayList for循环返回对象成员变量的错误值

时间:2018-05-03 14:04:52

标签: java arraylist

我试图编写一个函数来接收一个对象及其包含的ArrayList。已传递的对象应该使其relationship值增加10并且每个ArrayList中的其他对象的relationship值应增加3.我已经多次重写了我的代码,但我还是无法输出我想要的值。下面,我已经包含了我的函数,我正在使用的测试方法以及输出值。任何人都可以指出我哪里错了吗? (编辑:测试方法旁边的注释中的预期值。)

来自MainTest.java的

@BeforeEach
public void setUp() throws Exception {
    baratheons = new ArrayList<Lord>();
    baratheons.add( new Lord("Robert", 15));
    baratheons.add( new Lord("Renly", -5));
    baratheons.add( new Lord("Stannis", 30));

    starks = new ArrayList<Lord>();
    starks.add( new Lord("Robb", -60));
    starks.add( new Lord("Eddard", 0));
    starks.add( new Lord("Jon", 90));
}

@AfterEach
public void tearDown() throws Exception {
    baratheons = null;
    starks = null;
}


public void testGratefulLord() {
    int x = baratheons.get(0).getRelationship();
    System.out.println("\n This marks the start of testGratefulLord -");
    baratheons.get(0).giveFief(baratheons, baratheons.get(0));
    assertEquals(baratheons.get(0).getRelationship(), (x+=10));
    //expected value for Robert.getRelationship is 25 
}

@Test
public void testAlliesApprove() {
    int x = starks.get(1).getRelationship();
    System.out.println("\n This marks the start of testAlliesApprove -");
    starks.get(0).giveFief(starks, starks.get(0));
    assertEquals(starks.get(1).getRelationship(), x+3);
    //expected value for Eddard.getRelationship is 3
}
来自Lord.java的

public void giveFief(ArrayList<Lord> arrayListLord, Lord lordGivenFief) {
    lordGivenFief.relationship += 10;

    ArrayList<Lord> temp = new ArrayList<Lord>();
    temp.add(lordGivenFief);
    arrayListLord.remove(lordGivenFief);

    for (Lord l : arrayListLord) { 
        l.setRelationship(relationship+=3);
        System.out.printf("%s has a relationship of %d. \n", l.lordName, l.relationship);
    }
}

输出值

这标志着testGratefulLord的开始 -

Renly的关系为28.

斯坦尼斯的关系为31.

这标志着testAlliesApprove的开始 -

爱德华的关系是-47。

Jon的关系为-44。

1 个答案:

答案 0 :(得分:1)

  

ArrayList中的每个其他对象的关系值应增加3

但那并不是你在做什么。当你打电话

baratheons.get(0).giveFief(baratheons, baratheons.get(0))

thislordGivenFief都指Lord("Robert", 15)

您的列表以

开头
arrayListLord: Lord("Robert", 15)
               Lord("Renly", -5)
               Lord("Stannis", 30)

然后将“Robert”递增10并将其从列表中删除:

temp:          Lord("Robert", 25)

arrayListLord: Lord("Renly", -5)                
               Lord("Stannis", 30)

在循环中,然后执行l.setRelationship(relationship+=3),这在逻辑上与

相同
this.relationship = this.relationship + 3
l.setRelationship( this.relationship )

因此,在第一次迭代后,值为

temp:          Lord("Robert", 28)

arrayListLord: Lord("Renly", 28)                
               Lord("Stannis", 30)

因此,在第二次迭代后,值为

temp:          Lord("Robert", 31)

arrayListLord: Lord("Renly", 28)                
               Lord("Stannis", 31)

为您提供您看到的输出:

This marks the start of testGratefulLord -
Renly has a relationship of 28.
Stannis has a relationship of 31.

如果你想将每个对象的值增加3,你应该已经完成​​了

l.setRelationship(l.getRelationship() + 3)

那样,结果就是

temp:          Lord("Robert", 25)

arrayListLord: Lord("Renly", -2)                
               Lord("Stannis", 33)
相关问题