foreach循环中的Foreach循环(Java)

时间:2013-07-19 15:09:25

标签: java loops

我有以下代码:

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()进行比较。问题是当组织名称只出现一次时,组织名称会附加到属于他们的每个角色的消息中。

2 个答案:

答案 0 :(得分: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

你的循环正在运行,你要修复你的课程

答案 1 :(得分: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");
        }
    }
}

编辑:您应该Set<TRole> roles = new LinkedHashSet<TRole>();成为TOrganization IMO

的成员