0

Suppose that I have a two dimensional array holding 8 teams (rows) and in each team there is 12-15 players. Is there a way to know the total amount of players exist in String teams[][] (WITHOUT looping)?

2

2 Answers 2

3

You can do it with streams:

long players = Arrays.stream(teams).flatMap(team -> Arrays.stream(team)).count();
Sign up to request clarification or add additional context in comments.

1 Comment

... which "loops" behind the scenes :-)
1

You have to do it manually. Use something like this:

    int count = 0;
    for(int i = 0; i < teams.length;  i++)
        for(int j = 0; j < teams[i].length; j++)
            if(a[i][j] != null)
                count++;
    return count;

This assumes that fields in the array that do not contain a team member are simply null.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.