如何计算Arraylist中的项目?

时间:2015-05-12 22:06:29

标签: java arraylist

我需要在MemberOfCongress ArrayList内计算和打印民主党人,共和党人和独立人士的人数。我已经知道我需要遍历列表,但是,我的问题是,我如何计算每种派对类型的数量? 这是我到目前为止所得到的。

public int countNumDemocrats()
{
    int numDemocrats = 0;
    int numRepblican = 0;
    int numIndepent = 0;
    for (MemberOfCongress MemeberParty : members) {
        if (party.is"D" ) {
            numDemocrats++;
        }
    }
    return numDemocrats;
}

2 个答案:

答案 0 :(得分:0)

Your loop is fine, just add in the other 2 cases, like:

$_SESSION["students"]["Max"]["note"]

Assuming party.is"D" checks if the current MemeberParty is a D or not. Personally I'd use something like else if (party.is"R"){ //increment R } else if (party.is"I"){ //increment I }

And you'll probably want to return an array

if (MemeberParty.party.equals("D")){

答案 1 :(得分:0)

Try this:

itemsPerPageOptions: function(){
    return [1, 2, 3, 4, 5, this.get('totalItems')];
}.property('totalItems')

The method, when called, for example, with public int countObjectsOfThePassedType(Class<?> clazz, List<?> objects) { int counter = 0; for (Object obj : objects) { if (obj.getClass() == clazz) { counter++; } } return counter; } as first parameter, will return the number of democrats in your list.