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










share|improve this question



























    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










    share|improve this question

























      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










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 15:03

























      asked Nov 11 at 14:55









      gesp

      133




      133






















          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.






          share|improve this answer




















          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer




















          • 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














          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.






          share|improve this answer




















          • 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












          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党