0

我有以下代码:

Set<TOrganization> organizations = new LinkedHashSet<TOrganization>();
Set<TRole> roles = new LinkedHashSet<TRole>();
StringBuilder message = new StringBuilder("Requested roles: " + "\n");

//I fill them up with names like Test org A, Test Role A 1

for(TOrganization org : organizations) {
    message.append(" - " + org.getName()+ "\n");
    for(TRole role : roles) {
        if(role.getOrganization().equals(org)) {
            message.append("   - " + role.getName()+ "\n");
        }
    }
}

我想打印出不同类别的角色,如下所示:

“ - 测试组织 A”
“ - 测试角色 A 1”
“ - 测试角色 A 2”
“ - 测试组织 B”
“ - 测试角色 B 1”
“ - 测试角色 B 2”

但我的代码总是在角色之前附加组织名称,如下所示:

“ - 测试组织 A”
“ - 测试角色 A 1”
“ - 测试组织 A”
“ - 测试角色 A 2”
“ - 测试组织 B”
“ - 测试角色 B 1”
“ - 测试组织 B”
“ - 测试角色 B 2"

它似乎message.append(" - " + org.getName()+ "\n");在第二个循环运行时被执行。这怎么可能?

编辑:我用 printlns 对其进行了测试,但角色名称很好。我没有接触 equals() 或 hashcode(),我只是将字符串与 equals() 进行比较。问题是组织名称被附加到属于它们的每个角色的消息中,而它们应该只出现一次。

4

2 回答 2

1

这是有效的

class TOrganization {

    private String name;

    public TOrganization(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof TOrganization) {
            if (((TOrganization) obj).getName().equals(name)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

}

class TRole {

    private TOrganization organization;
    private String name;

    public TRole(TOrganization organization, String name) {
        this.organization = organization;
        this.name = name;
    }

    public TOrganization getOrganization() {
        return organization;
    }

    public String getName() {
        return name;
    }

}

public static void main(String[] args) {
    Set<TOrganization> organizations = new LinkedHashSet<TOrganization>();
    Set<TRole> roles = new LinkedHashSet<TRole>();
    StringBuilder message = new StringBuilder("Requested roles: " + "\n");

    TOrganization orga = new TOrganization("Test org A");
    TOrganization orgb = new TOrganization("Test org B");

    organizations.add(new TOrganization("Test org A"));
    organizations.add(new TOrganization("Test org B"));
    roles.add(new TRole(orga, "Test Role A 1"));
    roles.add(new TRole(orga, "Test Role A 2"));
    roles.add(new TRole(orgb, "Test Role B 1"));
    roles.add(new TRole(orgb, "Test Role B 2"));

    for (TOrganization org : organizations) {
        message.append(" - " + org.getName() + "\n");
        for (TRole role : roles) {
            if (role.getOrganization().equals(org)) {
                message.append("   - " + role.getName() + "\n");
            }
        }
    }
    System.out.println(message.toString());
}

输出

Requested roles: 
 - Test org A
   - Test Role A 1
   - Test Role A 2
 - Test org B
   - Test Role B 1
   - Test Role B 2

你的循环正在工作,你应该修复你的课程

于 2013-07-19T15:49:03.897 回答
0

您必须在外循环中分配roles(以便在变化时org变化)

for(TOrganization org : organizations) {
    message.append(" - " + org.getName()+ "\n");

    // like this, you adapt ;)
    roles = org.getRoles();

    for(TRole role : roles) {
        if(role.getOrganization().equals(org)) {
            message.append("   - " + role.getName()+ "\n");
        }
    }
}

编辑:你应该成为IMOSet<TRole> roles = new LinkedHashSet<TRole>();的成员TOrganization

于 2013-07-19T15:32:40.980 回答