Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes Referencing an array via a method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Referencing an array via a method." Watch "Referencing an array via a method." New topic
Author

Referencing an array via a method.

Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
As above, I'm trying to reference an array via a method in the following code, but it is not acting as I expect it to.

I am trying to update array element zero only, but I'm either updating both, or referencing both somehow, and for the life of me I can't see how

The following code results in the following output:

!!! BEFORE + GOLD
PlayerGold 0 = 30
PlayerGold 1 = 30
PlayerGold 2 = 30
!!! AFTER + GOLD
PlayerGold 0 = 40
PlayerGold 1 = 40
PlayerGold 2 = 30

i.e. both PlayerGold 1 & PlayerGold 2 seem to be getting updated



pD is an array of class Player which has been created and populated at startup e.g.
public Players pD = new Players();
pD.loadPlayers();



final boolean FoundGold() {
System.out.println("!!! BEFORE + GOLD");

System.out.println("PlayerGold 0 = " + pD.Invent(0)[gV.GOLD]);
System.out.println("PlayerGold " + pD.playerNum + " = " + pD.Invent(pD.playerNum)[gV.GOLD]);
System.out.println("PlayerGold " + (pD.playerNum+1) + " = " + pD.Invent(pD.playerNum+1)[gV.GOLD]);

pD.InventAdd(gV.GOLD, 10);
System.out.println("!!! AFTER + GOLD");

System.out.println("PlayerGold 0 = " + pD.Invent(0)[gV.GOLD]);
System.out.println("PlayerGold " + pD.playerNum + " = " + pD.Invent(pD.playerNum)[gV.GOLD]);
System.out.println("PlayerGold " + (pD.playerNum+1) + " = " + pD.Invent(pD.playerNum+1)[gV.GOLD]);


boolean fGold = true;
}








public class Players {

GlobalVariables gV = new GlobalVariables();
private static final int NUMPLAYERS = 6; //4PLAYERS+TEMP+DRAGON

public Player[] players;
int numPlayers = 1;
int playerNum;
int cursedPlayer = 1;

/** Creates a new instance of Players */
public Players() {
}

public void loadPlayers(){
players = new Player[NUMPLAYERS];

for (int i=0; i<players.length; ++i) {
players[i] = new Player();
}

players[5].invent[gV.GOLD] = 0; //Dragon!!!
players[5].invent[gV.WARRIOR] = 0; //Dragon!!!

}

public int[] Invent(int inventPlayerNum) {
return players[inventPlayerNum].invent;
}

public void InventAdd(int itemID, int incAmount) {
//players[0].invent[itemID] += incAmount;
//if (players[0].invent[itemID] > 99) players[0].invent[itemID] = 99;
//if (players[0].invent[itemID] < 0) players[0].invent[itemID] = 0;
//Doesn't work the above way either

Invent(0)[itemID] += incAmount;
if (Invent(0)[itemID] > 99) Invent(0)[itemID] = 99;
if (Invent(0)[itemID] < 0) Invent(0)[itemID] = 0;
}


public class Player {
public int score = 0;
public int[] foundItems = null;

//public int gold = 30;
//public int warriors = 10;
//public int food = 25;
//public boolean[] boughtItems = new boolean[3];
//public int[] invent = new int[6];

public int[] invent = {0,0,0,10,25,30,0,0,0,0,0};

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

public void SetTheScore () {
int score = 140 - (moves + 1 + invent[gV.WARRIOR]);
//Add extra 1 to move as move @ end of turn

if (score > 99) score = 99;
if (score < 0) score = 0;
}
}
}
[ January 15, 2007: Message edited by: Paul Carter ]
Joel Jorgensen
Greenhorn

Joined: Oct 13, 2006
Posts: 22


For starters, it looks like your debugging code may not be showing you what you think it is; it looks like you are actually retrieving the values for PlayerGold 0 and PlayerGold 1 from the exact same place: pD.Invent(pD.playerNum)[gV.GOLD]

Since (as far as I can tell) you do not change pD.playerNum between those two System.out.println() statements, it makes sense that they are printing out the same value. If I'm understanding it correctly, you'll need to fix that before you can say for sure whether the pD.InventAdd() method call is doing what you want it to do.


Go is to the fountain.
Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
Sorry - note to self "must not edit code in posts" :roll:

Your right the first line should be showing zero, but the result is the same

Regards

Paul.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Referencing an array via a method.
 
Similar Threads
How should I return an array of a class type?
Why am I getting NullPointerException?
Incompatibe Types & Symbol Not Found
What's the best way to clear an array.
app doesn't exit because of JOptionPane