-2

I am working on a code for one of my projects and it requires me to write an array. How do I achieve comment 5a? And did I get comment 5 right?

// 4. declare an array of players

Player [] team = new Player[];

// 5. loop over teamsize

for( int index = 0; index < team.length; index++)

// 5a. instantiate the i'th team member
4
  • For questions like these, it's better for you to read up on some Java tutorials instead of asking on StackOverflow. Commented Jul 16, 2015 at 2:41
  • Yes and team[index] = new Player(/* Fill with arguments */); Commented Jul 16, 2015 at 2:42
  • 1
    However, SO is generally for code you're having problems with, not code you don't know how to use. Google is for that. :) Commented Jul 16, 2015 at 2:43
  • possible duplicate of How to initialize an array of objects in Java and How to initialize an array of objects?.. Commented Jul 16, 2015 at 2:43

1 Answer 1

2

Yes you did get 5 right but I would suggest something like:

int length = team.length // to avoid unnecessarily calling it every iteration
for (int index = 0 ; index < length ; index++) {
   // ...
}

For 5a, just instantiate a Player class. If you don't know how to do that, read the Java tutorials. They are very helpful.

Just another thing for 5a, the instruction says i'th team member, this just means nth (because i is the common variable name for index which is what you are already using)

Sign up to request clarification or add additional context in comments.

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.