TreeSet仅为最后一个成员更改/更新大小

时间:2017-04-28 22:45:45

标签: java collections

所以我必须开发这个应用程序:

enter image description here

在我的班级"参与"我有这个toString:

@Override
public String toString() {
    return "[member=" + member.getNameOfMember() + " of " 
           + member.getLocationOfMember()+ ", article=" 
           + article.getName()+ ", party with " 
           + party.getCollParticipation().size() + "members" +"]";
}

我的党派,其中collParticipation位于:

package pkgData;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.TreeSet;

public class Party implements Serializable, Comparable<Party>{

    private static final long serialVersionUID = 1L;
    private LocalDate date;
    private TreeSet<Participation> collParticipation;

    public Party(LocalDate date) {
        super();
        this.date = date;
        collParticipation = new TreeSet<>();
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public TreeSet<Participation> getCollParticipation() {
        return collParticipation;
    }

    public void setCollParticipation(TreeSet<Participation> collParticipation) {
        this.collParticipation = collParticipation;
    }

    public void addParticipant(Participation p) throws Exception{
        if(collParticipation.contains(p))
            throw new Exception ("this member is already stored!!");

        if(date.compareTo(p.getParty().getDate())!=0)
            throw new Exception ("date of party of member is not same with party");

        collParticipation.add(p);
        System.out.println(collParticipation);
    }

    @Override
    public int compareTo(Party o) {
        return this.getDate().compareTo(o.getDate());
    }   
}

在这里我添加了一个新派对:

public void addParty(Party p) throws Exception{
    if(!(collParties.contains(p))){
        collParties.add(p);
    }else{
        Party actParty = collParties.floor(p);      
        for(Participation pcp : p.getCollParticipation()){
           actParty.addParticipant(pcp);  
        }
      } 
    }

当我添加派对时:

    Party newParty = new Party(LocalDate.parse(ftfDateOfParty.getText())); 
    Article a = db.getCollArticels().floor(new Article(nameOfArticle,12,"irgendwas"));
    //splitted String for Name, Location    
    Member m = new Member(splittedMember[0],splittedMember[1]);         
    Participation pm = new Participation(m,a,newParty); 

    newParty.addParticipant(pm);
    db.addParty(newParty);

所以问题是: 如果我将3个成员添加到同一个聚会,则输出总是如下:

[[member=San of KLfn, article=Buch, party with 1members],
 [member=San of k, article=Buch, party with 1members], 
 [member=Messi of KLfn, article=Buch, party with 3members]]

如果我从方法addParticipant打印更多参与者,我会得到这样的结果:

[[member=uj of ji, article=Buh, party with 1members]
,[member=uj of jdi, article=Buh, party with 1members]
,[member=uj of jdi, article=Buh, party with 1members]
,[member=uj of ji, article=Buh, party with 2members]]

以前成员的大小始终重置为1,只有最后一个成员具有partymembers的实际大小。我希望每个成员都有相同规模的党员。

修改

显然它只适用于此:

public void addParty(Party p) throws Exception{
    if(!(collParties.contains(p))){
        collParties.add(p);
    }else{
        Party actParty = collParties.floor(p);      
        for(Participation pcp : p.getCollParticipation()){
           actParty.addParticipant(pcp);  
        }
        for(Participation pcp : actParty.getCollParticipation()){
               pcp.setParty(actParty);
        }   
}

我必须设置&#34; pcp&#34;到了实际的派对。但是为什么我得到的结果是最后一个成员具有树集的实际长度,就像他使用actParty一样。难道他也不应该有1?

的长度

0 个答案:

没有答案