通过向量添加

时间:2013-12-13 13:33:09

标签: java object vector addition

public static void main(String[] args)
    {
        Vector vec = new Vector();

        vec.add(new Team(1, "Manchester City", 38, 64, 89));
        vec.add(new Team(2, "Manchester United", 38, 56, 89));
        vec.add(new Team(3, "Arsenal", 38, 25, 70));
        vec.add(new Team(4, "Tottenham", 38, 25, 69));
        vec.add(new Team(5, "Newcastle", 38, 5, 65));

        int points = 0;
        int total = 0;

        for(int i = 0; i < vec.size(); i++)
        {
            points = ((Team) vec.elementAt(i)).getPoints();
            total += points;
        }
        System.out.println("Total Points: " + points);


    }

任何人都可以在这里帮助我,我想要做的就是在我的对象中添加最后一个参数的所有值。

我下面的内容只是打印出最后一个对象的值(65)。

我会说它有点小我做错了但如果有人能指出我的话,那就太好了。

2 个答案:

答案 0 :(得分:2)

 System.out.println("Total Points: " + points);
                                        |
                                        look here it should be total.

这样改变。

 System.out.println("Total Points: " + total);

更改

points = ((Team) vec.elementAt(i)).getPoints();
total += points;

points += ((Team) vec.elementAt(i)).getPoints();
total = points;

答案 1 :(得分:2)

您可以使用foreach而不是:

for(Object t:vec)
{
    total += ((Team)t).getPoints();
}
相关问题