-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
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • For questions like these, it's better for you to read up on some Java tutorials instead of asking on StackOverflow. – almightyGOSU Jul 16 '15 at 02:41
  • Yes and `team[index] = new Player(/* Fill with arguments */);` – Jay Harris Jul 16 '15 at 02: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. :) – jfdoming Jul 16 '15 at 02:43
  • possible duplicate of [How to initialize an array of objects in Java](http://stackoverflow.com/questions/5889034/how-to-initialize-an-array-of-objects-in-java) and [How to initialize an array of objects?](http://stackoverflow.com/questions/19198196/how-to-initialize-an-array-of-objects).. – almightyGOSU Jul 16 '15 at 02:43

1 Answers1

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)

Rey Libutan
  • 5,226
  • 9
  • 42
  • 73