Jagged array confusion
up vote
1
down vote
favorite
I wanted to make a jagged array matrices that would store nine 1d arrays that would represent a matrice of a numbers from 0-9. I didn't wrote all the numbers from zero-nine, but all of them have the same size and the only difference is the position of 1s and 0s. This part was sucessful and once I printed the result of InitializeMatrices()
i indeed got the result that I wanted. matrices[0][34] would print the 35th element of the "zero" matrix, matrice[1][2] would print the 3th element of the "one" matrix and so on.
The problem began when inside DistortMatrices()
I tried to make a copies of matrice (36 copies, where indexes 0-17 would be copies of zero matrix) inside matrixToLearn, so I would modify the single copy of the matrice and not the matrice itself and store the changes insides the matrixToLearn. Sadly it doesn't seems to work the way I imagined. For example
matrixToLearn[0] = matrices[0]; // it copies the array zero as intended
matrixToLearn[0][34] = 0; //It applies to matrices[0][34] as well as all the 18 copies of matrixToLearn, even if after that i call:
matrixToLearn[17][7] = 0 //it will apply to matrices[0][34], matrixToLearn[0][34] and all the copies will be the same
So it seems that it works as some kind of pointer or smth.
How can I fix it so it would copy the matrices[0] to matrixtoLearn[0-17] and the changes to matrixToLearn[0-17] would apply only to itself and its corresponding index and leave the matrices as it is? Look at ---> Expected results under the code.
public partial class Form1 : Form
int zero, one, two, three, four, five, six, seven, eight, nine;
int matrices;
int matrixToLearn = new int[36];
private void InitializeMatrices()
zero =
new int
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
;
//And so on up to nine
matrices = new int zero, one, two, three, four, five, six, seven, eight, nine;
//index = 0
private void DistortMatrices(int index)
int z = 0;
for (; z < 18; z++)
matrixToLearn[z] = matrices[index];
matrixToLearn[0][34] = 0;
matrixToLearn[17][7] = 1;
Expected results
Expected result
matrixToLearn[0]
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrixToLearn[17]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
matrice[0]
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
How it looks right now:
matrixToLearn[0]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrixToLearn[17]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrices[0]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
EDIT: It seems to go even further and it modifies the "zero" matrix as well
c# arrays jagged-arrays
add a comment |
up vote
1
down vote
favorite
I wanted to make a jagged array matrices that would store nine 1d arrays that would represent a matrice of a numbers from 0-9. I didn't wrote all the numbers from zero-nine, but all of them have the same size and the only difference is the position of 1s and 0s. This part was sucessful and once I printed the result of InitializeMatrices()
i indeed got the result that I wanted. matrices[0][34] would print the 35th element of the "zero" matrix, matrice[1][2] would print the 3th element of the "one" matrix and so on.
The problem began when inside DistortMatrices()
I tried to make a copies of matrice (36 copies, where indexes 0-17 would be copies of zero matrix) inside matrixToLearn, so I would modify the single copy of the matrice and not the matrice itself and store the changes insides the matrixToLearn. Sadly it doesn't seems to work the way I imagined. For example
matrixToLearn[0] = matrices[0]; // it copies the array zero as intended
matrixToLearn[0][34] = 0; //It applies to matrices[0][34] as well as all the 18 copies of matrixToLearn, even if after that i call:
matrixToLearn[17][7] = 0 //it will apply to matrices[0][34], matrixToLearn[0][34] and all the copies will be the same
So it seems that it works as some kind of pointer or smth.
How can I fix it so it would copy the matrices[0] to matrixtoLearn[0-17] and the changes to matrixToLearn[0-17] would apply only to itself and its corresponding index and leave the matrices as it is? Look at ---> Expected results under the code.
public partial class Form1 : Form
int zero, one, two, three, four, five, six, seven, eight, nine;
int matrices;
int matrixToLearn = new int[36];
private void InitializeMatrices()
zero =
new int
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
;
//And so on up to nine
matrices = new int zero, one, two, three, four, five, six, seven, eight, nine;
//index = 0
private void DistortMatrices(int index)
int z = 0;
for (; z < 18; z++)
matrixToLearn[z] = matrices[index];
matrixToLearn[0][34] = 0;
matrixToLearn[17][7] = 1;
Expected results
Expected result
matrixToLearn[0]
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrixToLearn[17]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
matrice[0]
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
How it looks right now:
matrixToLearn[0]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrixToLearn[17]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrices[0]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
EDIT: It seems to go even further and it modifies the "zero" matrix as well
c# arrays jagged-arrays
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I wanted to make a jagged array matrices that would store nine 1d arrays that would represent a matrice of a numbers from 0-9. I didn't wrote all the numbers from zero-nine, but all of them have the same size and the only difference is the position of 1s and 0s. This part was sucessful and once I printed the result of InitializeMatrices()
i indeed got the result that I wanted. matrices[0][34] would print the 35th element of the "zero" matrix, matrice[1][2] would print the 3th element of the "one" matrix and so on.
The problem began when inside DistortMatrices()
I tried to make a copies of matrice (36 copies, where indexes 0-17 would be copies of zero matrix) inside matrixToLearn, so I would modify the single copy of the matrice and not the matrice itself and store the changes insides the matrixToLearn. Sadly it doesn't seems to work the way I imagined. For example
matrixToLearn[0] = matrices[0]; // it copies the array zero as intended
matrixToLearn[0][34] = 0; //It applies to matrices[0][34] as well as all the 18 copies of matrixToLearn, even if after that i call:
matrixToLearn[17][7] = 0 //it will apply to matrices[0][34], matrixToLearn[0][34] and all the copies will be the same
So it seems that it works as some kind of pointer or smth.
How can I fix it so it would copy the matrices[0] to matrixtoLearn[0-17] and the changes to matrixToLearn[0-17] would apply only to itself and its corresponding index and leave the matrices as it is? Look at ---> Expected results under the code.
public partial class Form1 : Form
int zero, one, two, three, four, five, six, seven, eight, nine;
int matrices;
int matrixToLearn = new int[36];
private void InitializeMatrices()
zero =
new int
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
;
//And so on up to nine
matrices = new int zero, one, two, three, four, five, six, seven, eight, nine;
//index = 0
private void DistortMatrices(int index)
int z = 0;
for (; z < 18; z++)
matrixToLearn[z] = matrices[index];
matrixToLearn[0][34] = 0;
matrixToLearn[17][7] = 1;
Expected results
Expected result
matrixToLearn[0]
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrixToLearn[17]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
matrice[0]
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
How it looks right now:
matrixToLearn[0]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrixToLearn[17]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrices[0]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
EDIT: It seems to go even further and it modifies the "zero" matrix as well
c# arrays jagged-arrays
I wanted to make a jagged array matrices that would store nine 1d arrays that would represent a matrice of a numbers from 0-9. I didn't wrote all the numbers from zero-nine, but all of them have the same size and the only difference is the position of 1s and 0s. This part was sucessful and once I printed the result of InitializeMatrices()
i indeed got the result that I wanted. matrices[0][34] would print the 35th element of the "zero" matrix, matrice[1][2] would print the 3th element of the "one" matrix and so on.
The problem began when inside DistortMatrices()
I tried to make a copies of matrice (36 copies, where indexes 0-17 would be copies of zero matrix) inside matrixToLearn, so I would modify the single copy of the matrice and not the matrice itself and store the changes insides the matrixToLearn. Sadly it doesn't seems to work the way I imagined. For example
matrixToLearn[0] = matrices[0]; // it copies the array zero as intended
matrixToLearn[0][34] = 0; //It applies to matrices[0][34] as well as all the 18 copies of matrixToLearn, even if after that i call:
matrixToLearn[17][7] = 0 //it will apply to matrices[0][34], matrixToLearn[0][34] and all the copies will be the same
So it seems that it works as some kind of pointer or smth.
How can I fix it so it would copy the matrices[0] to matrixtoLearn[0-17] and the changes to matrixToLearn[0-17] would apply only to itself and its corresponding index and leave the matrices as it is? Look at ---> Expected results under the code.
public partial class Form1 : Form
int zero, one, two, three, four, five, six, seven, eight, nine;
int matrices;
int matrixToLearn = new int[36];
private void InitializeMatrices()
zero =
new int
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
;
//And so on up to nine
matrices = new int zero, one, two, three, four, five, six, seven, eight, nine;
//index = 0
private void DistortMatrices(int index)
int z = 0;
for (; z < 18; z++)
matrixToLearn[z] = matrices[index];
matrixToLearn[0][34] = 0;
matrixToLearn[17][7] = 1;
Expected results
Expected result
matrixToLearn[0]
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrixToLearn[17]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
matrice[0]
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
How it looks right now:
matrixToLearn[0]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrixToLearn[17]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
matrices[0]
1, 1, 1, 1, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 0,
EDIT: It seems to go even further and it modifies the "zero" matrix as well
c# arrays jagged-arrays
c# arrays jagged-arrays
edited Nov 11 at 15:03
asked Nov 11 at 14:55
gesp
133
133
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
matrixToLearn[0] = matrices[0];
This copies the reference to a nested array. It is just like
float x = ...;
float y = x; //copies the reference, not the object contents
I usually use (float)myArray.Clone()
for cloning arrays. I make this into a little generic extension method.
Thank you, that makes sense: Changing:matrixToLearn[z] = matrices[index];
to:matrixToLearn[z] = (int) matrices[index].Clone();
fixed the problem. At least it seems so after printing all the involved arrays after doing these operations. Just gotta make extra sure but i think it will be the right solution!
– gesp
Nov 11 at 16:02
Checked everything a few times and it does work! Thank you very much. The problem is solved.
– gesp
Nov 11 at 17:05
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
matrixToLearn[0] = matrices[0];
This copies the reference to a nested array. It is just like
float x = ...;
float y = x; //copies the reference, not the object contents
I usually use (float)myArray.Clone()
for cloning arrays. I make this into a little generic extension method.
Thank you, that makes sense: Changing:matrixToLearn[z] = matrices[index];
to:matrixToLearn[z] = (int) matrices[index].Clone();
fixed the problem. At least it seems so after printing all the involved arrays after doing these operations. Just gotta make extra sure but i think it will be the right solution!
– gesp
Nov 11 at 16:02
Checked everything a few times and it does work! Thank you very much. The problem is solved.
– gesp
Nov 11 at 17:05
add a comment |
up vote
1
down vote
accepted
matrixToLearn[0] = matrices[0];
This copies the reference to a nested array. It is just like
float x = ...;
float y = x; //copies the reference, not the object contents
I usually use (float)myArray.Clone()
for cloning arrays. I make this into a little generic extension method.
Thank you, that makes sense: Changing:matrixToLearn[z] = matrices[index];
to:matrixToLearn[z] = (int) matrices[index].Clone();
fixed the problem. At least it seems so after printing all the involved arrays after doing these operations. Just gotta make extra sure but i think it will be the right solution!
– gesp
Nov 11 at 16:02
Checked everything a few times and it does work! Thank you very much. The problem is solved.
– gesp
Nov 11 at 17:05
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
matrixToLearn[0] = matrices[0];
This copies the reference to a nested array. It is just like
float x = ...;
float y = x; //copies the reference, not the object contents
I usually use (float)myArray.Clone()
for cloning arrays. I make this into a little generic extension method.
matrixToLearn[0] = matrices[0];
This copies the reference to a nested array. It is just like
float x = ...;
float y = x; //copies the reference, not the object contents
I usually use (float)myArray.Clone()
for cloning arrays. I make this into a little generic extension method.
answered Nov 11 at 15:05
usr
143k26185297
143k26185297
Thank you, that makes sense: Changing:matrixToLearn[z] = matrices[index];
to:matrixToLearn[z] = (int) matrices[index].Clone();
fixed the problem. At least it seems so after printing all the involved arrays after doing these operations. Just gotta make extra sure but i think it will be the right solution!
– gesp
Nov 11 at 16:02
Checked everything a few times and it does work! Thank you very much. The problem is solved.
– gesp
Nov 11 at 17:05
add a comment |
Thank you, that makes sense: Changing:matrixToLearn[z] = matrices[index];
to:matrixToLearn[z] = (int) matrices[index].Clone();
fixed the problem. At least it seems so after printing all the involved arrays after doing these operations. Just gotta make extra sure but i think it will be the right solution!
– gesp
Nov 11 at 16:02
Checked everything a few times and it does work! Thank you very much. The problem is solved.
– gesp
Nov 11 at 17:05
Thank you, that makes sense: Changing:
matrixToLearn[z] = matrices[index];
to: matrixToLearn[z] = (int) matrices[index].Clone();
fixed the problem. At least it seems so after printing all the involved arrays after doing these operations. Just gotta make extra sure but i think it will be the right solution!– gesp
Nov 11 at 16:02
Thank you, that makes sense: Changing:
matrixToLearn[z] = matrices[index];
to: matrixToLearn[z] = (int) matrices[index].Clone();
fixed the problem. At least it seems so after printing all the involved arrays after doing these operations. Just gotta make extra sure but i think it will be the right solution!– gesp
Nov 11 at 16:02
Checked everything a few times and it does work! Thank you very much. The problem is solved.
– gesp
Nov 11 at 17:05
Checked everything a few times and it does work! Thank you very much. The problem is solved.
– gesp
Nov 11 at 17:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249934%2fjagged-array-confusion%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown