• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How should I return an array of a class type?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic