Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am programming a little game and have a problem now:
The integer array yourField[0][0] has the value 0
The integer array opponentsField[0][0] has also the value 0
row and column are 0
opponentsField[row][column - 13] = 2;

after this code the value opponentsField[0][0] has of course changed to 2. But also the value of yourField[0][0] has changed to 2. What could be a reason for that? It is really weird and I have no idea why it happens.
Thank you for answers!

What I have tried:

I have tried to see what happens with the debugger.
Posted
Updated 28-Jan-17 5:56am
v2
Comments
Thomas Daniels 28-Jan-17 11:15am    
How are opponentsField and yourField defined? Can you show these lines of code?
Peter Leow 28-Jan-17 11:32am    
Read your question from the point of a stranger. Are you able to understand your question?
Richard MacCutchan 28-Jan-17 11:50am    
They are most likely the same array. You need to declare them separately.
OnLearn 28-Jan-17 12:23pm    
I haven't done it like yourField = opponentsField;
I have a third array called field[][].
Then I built yourField like this: yourField[][]=Arrays.copyOf(field[][],10);
I gave field other values and built opponentsField equally.

If I initialize the array with copyOf() it isn't a pointer to an array, is it?
Richard MacCutchan 28-Jan-17 12:37pm    
I'm still not sure what you are doing. Please edit your question and show the actual code so we can see the sequence of events clearly.

At a guess, when you created the array variables, either you assigned them both to the same area of memory, or you didn't assign them at all.
If you create your arrays like this:
int[] anArray;
anArray = new int[10];
int[] anotherArray = anArray;

then both array variables anArray and anotherArray share the same memory - so anything you do via one affect the memory the other uses.
You need to assign them separately:
int[] anArray;
anArray = new int[10];
int[] anotherArray;
anotherArray = new int[10];
to get separate memory for each.
 
Share this answer
 
Comments
OnLearn 28-Jan-17 12:23pm    
I haven't done it like yourField = opponentsField;
I have a third array called field[][].
Then I built yourField like this: yourField[][]=Arrays.copyOf(field[][],10);
I gave field other values and built opponentsField equally.

If I initialize the array with copyOf() it isn't a pointer to an array, is it?
yourField and opponentsField are not arrays, they are pointers to an array.
And my guess is that you built 1 from the other
Java
yourField= opponentsField

making both pointers to the same array.

You need to improve your knowledge of Java.
 
Share this answer
 
v2
Comments
OnLearn 28-Jan-17 12:23pm    
I haven't done it like yourField = opponentsField;
I have a third array called field[][].
Then I built yourField like this: yourField[][]=Arrays.copyOf(field[][],10);
I gave field other values and built opponentsField equally.

If I initialize the array with copyOf() it isn't a pointer to an array, is it?
Patrice T 28-Jan-17 12:27pm    
Show related code.
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900