Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How should I return an array of a class type?  RSS feed

 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a method 'GetPlayers' that returns an array of Class 'Player' inside the Class itself - Good idea or bad idea?

If good, how would I use the method to create an instance of the array?

e.g.
Player players = new Player.GetPlayers();
(yes it's still early days for me :rolleyes

Many thanks

Paul.



public class Player {
private int gold = 30;
private int warriors = 10;
private int food = 25;

private int[] boughtItems = null;
private int[] foundItems = null;

private int kingdom = 0;
private int moves = 0;
private int lastLocation = 0;
private boolean cursed = false;

public Player[] GetPlayers(){
Player players[];
players = new Player[6]; //CurrPlayer, 4xPlayers + Dragon

for (int i=0; i<players.length; ++i)
players[i] = new Player();
players[5].gold = 0; //Dragon!!!
players[5].warriors = 0; //Dragon!!!

return players;
}
}
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to expand on Tim's comment, after you create the players array, you need to put some players IN the arrray (assuming you want some in there. you'd need something like

players[0] = new Player();
players[1] = new Player();
etc.

this could be done with a loop as well. the point is that the line

Player[] players = new Player[5];

creates an empty array that can hold up to 5 players. you need to populate the array however works for you.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
your method is ok, only looks a bit weird due to missing indentation.

But your call is wrong, should be like




Yours,
Bu.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect a getPlayers method to simply return a reference to a private field (an array of Players that's being maintained in that instance) and not actually "do" anything to the Players in that array...

Separate methods might initialize (reset) or modify this array...

Does that make sense?
 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect a getPlayers method to simply return a reference to a private field (an array of Players that's being maintained in that instance) and not actually "do" anything to the Players in that array...

Separate methods might initialize (reset) or modify this array...


I see what you're saying.

Would these manipulating methods be okay defined in the Player class?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, that's how you access private data in a class,
you just create a public function/method to modify, return,
or update that piece of private data..

now say you have a private static player[], and it's member data.

then you could make a public static Player[] getPlayers(){}

method, that stores the current Player[] in your private class
into a new array in the client.

ex:
Player [] a = Player.getPlayers();


If I am wrong, or off track, please let me know

Justin


LOL just ignore this post...
[ October 12, 2006: Message edited by: Justin Fox ]
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Carter:


I see what you're saying.

Would these manipulating methods be okay defined in the Player class?


If the class is Player and not plural, I would expect all non-static methods to return only data about itself, a single player. If you need to have a group of players, it should be done outside that class.

Would you expect a JButton object to return all JButton objects in a GUI? Or a single Integer object to return all Integers that have been created?
[ October 12, 2006: Message edited by: Rusty Shackleford ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!