[英]Java two dimensional Array of ArrayList
对于学校,我必须根据荷兰的投票系统制定投票计划,那里有有候选人的政党。 对于这个程序,我制作了一个“候选人”类,其中有一个用于获取候选人姓名的getter和setter方法。 然后是一个“ Party”类,其中包含:
ArrayList<Candidate>CandidateList
以及按名称添加候选人的方法。 接下来,我制作了一个“ PartyList”类,其中包含:
ArrayList<Party>Parties
和这种方法:
public void addParty(Party party){
Parties.add(new Party());
我认为最好这样做:
ArrayList<ArrayList<Party>>Parties
但是我的老师说制作一个一维ArrayList就足够了。 现在是我迷路的部分:
我还有另一个“投票”类别,在该类别中进行最终投票,但是为此,我必须对当事方和候选人进行二维排列,如下所示:
1 1
1 2
1 3
2 1
2 2
3 1
etc.
其中第一列代表政党,第二列代表候选人。 我知道第一列可以通过使用Parties.size()来实现,但是第二列则不能这样,因为CandidateList的数组列表不止一个。 我怎样才能做到这一点?
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;
}
}
像这样使用它来存储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.