Results 1 to 11 of 11
Thread: passing an array between classes
- 04-10-2009, 02:03 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 26
- Rep Power
- 0
passing an array between classes
Hi,
Ok when i pass my array to another class it modifies the array. i am not sure why. I get the value of the array from a class and then i pass the value of the array to a different class. when i then modify the values in the class it changes the original values of the array. Hope that makes sense.
Many thanks
Gisler
- 04-10-2009, 02:11 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
It's probably because the array you think to modify actually points to the original array. What you should do is use a loop (or System.arraycopy) to copy all values in the original array to the array to be modified.
I die a little on the inside...
Every time I get shot.
- 04-10-2009, 02:12 PM #3
Couple of things...
First you say that you are passing an array between classes:
Then you say you are passing an array value between classes:when i pass my array to another class it modifies the array
Which is it?I get the value of the array from a class and then i pass the value of the array to a different class.
I would suggest you post some code showing what you are trying to do.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-10-2009, 02:14 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 20
- Rep Power
- 0
- 04-10-2009, 02:22 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 26
- Rep Power
- 0
Hi, thanks for the quick reply. Here is some of my code
randomNumbers = newGame.getarray(); //gets the array
totalGuesses++;
guessNumbers[0]= guessN1.getNumber();
guessNumbers[1]= guessN2.getNumber();
guessNumbers[2]= guessN3.getNumber();
guessNumbers[3]= guessN4.getNumber();
results = calculateGuess.getGuessresults(guessNumbers ,randomNumbers);
score=(results[2]/totalGuesses)+score;
the first line gets the values of the array and puts it into a new array called randomNumbers. I then pass the values of the array randomNumbers and guessNumbers to a new class called calculateGuess. The methods for that class is...
public int[] getGuessresults(int[] guessed,int [] randomNum)
{
results = calculateResults(guessed, randomNum);
return results;
}
private int[] calculateResults(int[] guesses,int[]randomNums)
{
//Edit the array here.
Thanks for your help.
gisler
- 04-10-2009, 02:32 PM #6
I'm still fuzzy about were the problem is. What array elements get changed? Have you tried to debug the problem (with printlns)? If not, try putting printlns throughout your code so you can follow the flow of data and find where it's being changed.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-10-2009, 02:46 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 26
- Rep Power
- 0
the problem is when i change the values of the array in my calculateGuess class
private int[] calculateResults(int[] guesses,int[]randomNums)
{
//Edit the array here.
it also changes the values of the array in my newgame class.
I have tried debugging this load and can't figure it out. i think it might have something to do with making my methods or classes public when they should be private.randomNumbers = newGame.getarray(); //gets the array
Many thanks
gisler
- 04-10-2009, 03:42 PM #8
Member
- Join Date
- Apr 2009
- Posts
- 20
- Rep Power
- 0
Can you please specify where the values are getting changed in your main class?
- 04-10-2009, 03:45 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 20
- Rep Power
- 0
One more thing...you can directly call calculateResults() method from your main class as nothing is done in getGuessresults() except you are calling calculateResults() method.
- 04-10-2009, 05:52 PM #10
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
- 04-10-2009, 10:31 PM #11
Member
- Join Date
- Apr 2009
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
help with passing objects between classes
By aruna1 in forum New To JavaReplies: 7Last Post: 03-22-2009, 02:41 PM -
Passing values between classes problem.
By alin_ms in forum New To JavaReplies: 8Last Post: 12-12-2008, 06:49 PM -
Need help passing data between classes
By bri1547 in forum New To JavaReplies: 3Last Post: 07-21-2008, 04:19 AM -
Passing variable information between classes
By zen_to_go in forum New To JavaReplies: 1Last Post: 10-30-2007, 08:09 PM -
passing an array into an instance
By lockmac in forum New To JavaReplies: 1Last Post: 08-08-2007, 09:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks