How to return the sum in an object ArrayList

Noah Kettler

Using a for each loop. how can I count the number of goals each player has and return that in the method goals() which is in the Team class? I know my current return statement is wrong I was unsure what to put there:

import java.util.ArrayList;

public class Team {

    private String teamName;
    private ArrayList<Player> list;
    private int maxSize = 16;

    public Team(String teamName) {
        this.teamName = teamName;
        this.list = new ArrayList<Player>();
    }

    public String getName() {

        return this.teamName;
    }

    public void addPlayer(Player player) {

        if (list.size() < this.maxSize) {
            this.list.add(player);
        }

    }

    public void printPlayers() {
        System.out.println(list);
    }

    public void setMaxSize(int maxSize) {

        this.maxSize = maxSize;
    }

    public int size() {

        return list.size();
    }

    public int goals(){

        for(Player goals : list){

        }
        return list;
    }
}

public class Player {

    private String playerName;
    private int goals;

    public Player(String playerName) {

        this.playerName = playerName;
    }

    public Player(String playerName, int goals) {

        this.playerName = playerName;
        this.goals = goals;
    }

    public String getName() {

        return this.playerName;
    }

    public int goals() {

        return this.goals;
    }

    public String toString() {

        return "Player: " + this.playerName + "," + goals;
    }
}

public class Main {
    public static void main(String[] args) {
        // test your code here
        Team barcelona = new Team("FC Barcelona");

        Player brian = new Player("Brian");
        Player pekka = new Player("Pekka", 39);
        barcelona.addPlayer(brian);
        barcelona.addPlayer(pekka);
        barcelona.addPlayer(new Player("Mikael", 1)); // works similarly as the above

        System.out.println("Total goals: " + barcelona.goals());
    }
}
Elliott Frisch

I think you're looking for something like

public int goals(){
    int total = 0;
    for(Player p : list){ // for each Player p in list         
       total += p.goals();
    }       
    return total;
}

Add the number of each Player's goals to the total and then return the total.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how sum of 2 arraylist?

From Dev

How does the Java class ArrayList return an Iterator Object?

From Dev

How can I return an object from a ArrayList? (JAVA)

From Dev

How to return a distinct object inside a Set and sum a common value?

From Dev

What object to return in the following arraylist

From Dev

How to return boolean statement if the sum of an arraylist is less than or equal to a maximum number?

From Dev

How to sum up the individual fields of the object list and return the results as a single object

From Dev

How to cast an Object to Arraylist

From Dev

Return object from arraylist with highest value?

From Dev

JAVA return ArrayList object from conditional

From Dev

Change an object within ArrayList using a method and return

From Dev

Return object from arraylist with highest value?

From Dev

Java: Extending ArrayList have add() return Object

From Dev

JAVA return ArrayList object from conditional

From Dev

How to return an ArrayList with an recursive function

From Dev

How to return an arraylist iterator? (Java)

From Dev

How to return an ArrayList with an recursive function

From Dev

How to populate an ArrayList<String> from ArrayList<Object>

From Dev

How to sum an ArrayList<Integers> using recursion?

From Dev

How to Convert a String ArrayList to a Double ArrayList and calculate the sum of values?

From Dev

How to create arraylist of object in swift

From Dev

How to add an object to an ArrayList in Java

From Dev

How to increment the index of an object in an ArrayList

From Dev

How to search for a StringBuffer object in an ArrayList?

From Dev

how to use Iterator in object arraylist

From Dev

How to search for a specific object in arraylist

From Dev

How to return an object that was deleted?

From Dev

How to return object?

From Dev

How to return object?

Related Related

  1. 1

    how sum of 2 arraylist?

  2. 2

    How does the Java class ArrayList return an Iterator Object?

  3. 3

    How can I return an object from a ArrayList? (JAVA)

  4. 4

    How to return a distinct object inside a Set and sum a common value?

  5. 5

    What object to return in the following arraylist

  6. 6

    How to return boolean statement if the sum of an arraylist is less than or equal to a maximum number?

  7. 7

    How to sum up the individual fields of the object list and return the results as a single object

  8. 8

    How to cast an Object to Arraylist

  9. 9

    Return object from arraylist with highest value?

  10. 10

    JAVA return ArrayList object from conditional

  11. 11

    Change an object within ArrayList using a method and return

  12. 12

    Return object from arraylist with highest value?

  13. 13

    Java: Extending ArrayList have add() return Object

  14. 14

    JAVA return ArrayList object from conditional

  15. 15

    How to return an ArrayList with an recursive function

  16. 16

    How to return an arraylist iterator? (Java)

  17. 17

    How to return an ArrayList with an recursive function

  18. 18

    How to populate an ArrayList<String> from ArrayList<Object>

  19. 19

    How to sum an ArrayList<Integers> using recursion?

  20. 20

    How to Convert a String ArrayList to a Double ArrayList and calculate the sum of values?

  21. 21

    How to create arraylist of object in swift

  22. 22

    How to add an object to an ArrayList in Java

  23. 23

    How to increment the index of an object in an ArrayList

  24. 24

    How to search for a StringBuffer object in an ArrayList?

  25. 25

    how to use Iterator in object arraylist

  26. 26

    How to search for a specific object in arraylist

  27. 27

    How to return an object that was deleted?

  28. 28

    How to return object?

  29. 29

    How to return object?

HotTag

Archive