简体   繁体   English

Java二维数组的ArrayList

[英]Java two dimensional Array of ArrayList

For school I have to make a voting program according to the Dutch voting system where there are parties which have candidates. 对于学校,我必须根据荷兰的投票系统制定投票计划,那里有有候选人的政党。 For this program I have made a class "Candidate" which has a getter and setter for the name of the candidate. 对于这个程序,我制作了一个“候选人”类,其中有一个用于获取候选人姓名的getter和setter方法。 Then there is a class "Party" which contains: 然后是一个“ Party”类,其中包含:

ArrayList<Candidate>CandidateList 

and a method to add candidates by name. 以及按名称添加候选人的方法。 Next, I made a class "PartyList" which contains: 接下来,我制作了一个“ PartyList”类,其中包含:

ArrayList<Party>Parties 

and this method: 和这种方法:

public void addParty(Party party){
    Parties.add(new Party());

I thought it would be better to do it like this: 我认为最好这样做:

ArrayList<ArrayList<Party>>Parties

but my teacher said it would be enough to make a one dimensional ArrayList. 但是我的老师说制作一个一维ArrayList就足够了。 Now comes the part where I get lost: 现在是我迷路的部分:

I have another class "Voting" in which the final voting takes place, but for that i have to make a two-dimensional array of the parties and candidates that will look like this: 我还有另一个“投票”类别,在该类别中进行最终投票,但是为此,我必须对当事方和候选人进行二维排列,如下所示:

1 1
1 2
1 3
2 1
2 2
3 1
etc.

In which the first column represents the party and the second represents the candidate. 其中第一列代表政党,第二列代表候选人。 I know that the first column would be possible by using Parties.size() but the second column wouldn't be possible this way because there are more than one Arraylists of CandidateList. 我知道第一列可以通过使用Parties.size()来实现,但是第二列则不能这样,因为CandidateList的数组列表不止一个。 How can i accomplish this? 我怎样才能做到这一点?

class Poll {
private Integer label;
private Integer value;

// Constructor or setter
public void Poll(Integer label, Integer value) {
    if (label == null || value == null) {
        return;
    }
    this.label = label;
    this.value = value;
}

// getters

public Integer getLabel() {
    return this.label;
}

public Integer getValue() {
    return this.value;
}

} }

use it like this to store 2 items rather than multidimensional stuff 像这样使用它来存储2个项目,而不是多维内容

private ArrayList<Poll> items = new ArrayList<Poll>();
items.add(new Stuff(label, value));
 for (Poll item: items) {
 doSomething(item.getLabel()); // returns Integer      
 doSomething(item.getValue()); // returns Integer
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM