Creating a 2 dimensional array with loops












1















I'm asking this question because I need to create a two dimensional array as the program is running. All the other questions on the website have data already inside the array, which is what I don't have so I can't follow those tutorials.



Prior to this code snippet, I've got all the variables initialized properly.



Excuse my formatting, this is the first question I've asked on here



for (int i = 0; i < numOfVals; i++){
numSpc = 50 - values[i];
for (int k = 0; k < 51; k++){
for (int j = 0; j < values[i]; j++){
twoDim[k[j]]=1;
}
for (int m = 0; m < numSpc; m ++){
twoDim[k[j]]=0;
}
}
}


What I'm trying to do here is make an array inside twoDim called k, and then edit the values in there. It want it to look like twoDim[[0,0,0,1,1,0,1,0,1], [1,1,1,0,0,0,1,0,1]]; except there would be 50 values in each internal array (k).



The problem is, I keep getting two different kinds of errors. One is saying it can't find the variable j (cannot find symbol) and the other error says array required, but int found about k. How can I fix this?










share|improve this question























  • 1. j's scope is limited only to its for-loop. 2. How have you defined twoDim?

    – Nicholas K
    Nov 18 '18 at 16:31











  • twoDim was defined with int twoDim = new int[numOfVals];. numOfVals is just an integer user input. I'll fix the scoping, thank you for point out that j is outside of it's loop in the second mention of it. I think i copy-pasted but forgot to change it to k[m] instead of k[j]

    – Zacki Aseer
    Nov 18 '18 at 16:39
















1















I'm asking this question because I need to create a two dimensional array as the program is running. All the other questions on the website have data already inside the array, which is what I don't have so I can't follow those tutorials.



Prior to this code snippet, I've got all the variables initialized properly.



Excuse my formatting, this is the first question I've asked on here



for (int i = 0; i < numOfVals; i++){
numSpc = 50 - values[i];
for (int k = 0; k < 51; k++){
for (int j = 0; j < values[i]; j++){
twoDim[k[j]]=1;
}
for (int m = 0; m < numSpc; m ++){
twoDim[k[j]]=0;
}
}
}


What I'm trying to do here is make an array inside twoDim called k, and then edit the values in there. It want it to look like twoDim[[0,0,0,1,1,0,1,0,1], [1,1,1,0,0,0,1,0,1]]; except there would be 50 values in each internal array (k).



The problem is, I keep getting two different kinds of errors. One is saying it can't find the variable j (cannot find symbol) and the other error says array required, but int found about k. How can I fix this?










share|improve this question























  • 1. j's scope is limited only to its for-loop. 2. How have you defined twoDim?

    – Nicholas K
    Nov 18 '18 at 16:31











  • twoDim was defined with int twoDim = new int[numOfVals];. numOfVals is just an integer user input. I'll fix the scoping, thank you for point out that j is outside of it's loop in the second mention of it. I think i copy-pasted but forgot to change it to k[m] instead of k[j]

    – Zacki Aseer
    Nov 18 '18 at 16:39














1












1








1








I'm asking this question because I need to create a two dimensional array as the program is running. All the other questions on the website have data already inside the array, which is what I don't have so I can't follow those tutorials.



Prior to this code snippet, I've got all the variables initialized properly.



Excuse my formatting, this is the first question I've asked on here



for (int i = 0; i < numOfVals; i++){
numSpc = 50 - values[i];
for (int k = 0; k < 51; k++){
for (int j = 0; j < values[i]; j++){
twoDim[k[j]]=1;
}
for (int m = 0; m < numSpc; m ++){
twoDim[k[j]]=0;
}
}
}


What I'm trying to do here is make an array inside twoDim called k, and then edit the values in there. It want it to look like twoDim[[0,0,0,1,1,0,1,0,1], [1,1,1,0,0,0,1,0,1]]; except there would be 50 values in each internal array (k).



The problem is, I keep getting two different kinds of errors. One is saying it can't find the variable j (cannot find symbol) and the other error says array required, but int found about k. How can I fix this?










share|improve this question














I'm asking this question because I need to create a two dimensional array as the program is running. All the other questions on the website have data already inside the array, which is what I don't have so I can't follow those tutorials.



Prior to this code snippet, I've got all the variables initialized properly.



Excuse my formatting, this is the first question I've asked on here



for (int i = 0; i < numOfVals; i++){
numSpc = 50 - values[i];
for (int k = 0; k < 51; k++){
for (int j = 0; j < values[i]; j++){
twoDim[k[j]]=1;
}
for (int m = 0; m < numSpc; m ++){
twoDim[k[j]]=0;
}
}
}


What I'm trying to do here is make an array inside twoDim called k, and then edit the values in there. It want it to look like twoDim[[0,0,0,1,1,0,1,0,1], [1,1,1,0,0,0,1,0,1]]; except there would be 50 values in each internal array (k).



The problem is, I keep getting two different kinds of errors. One is saying it can't find the variable j (cannot find symbol) and the other error says array required, but int found about k. How can I fix this?







java arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 16:28









Zacki AseerZacki Aseer

62




62













  • 1. j's scope is limited only to its for-loop. 2. How have you defined twoDim?

    – Nicholas K
    Nov 18 '18 at 16:31











  • twoDim was defined with int twoDim = new int[numOfVals];. numOfVals is just an integer user input. I'll fix the scoping, thank you for point out that j is outside of it's loop in the second mention of it. I think i copy-pasted but forgot to change it to k[m] instead of k[j]

    – Zacki Aseer
    Nov 18 '18 at 16:39



















  • 1. j's scope is limited only to its for-loop. 2. How have you defined twoDim?

    – Nicholas K
    Nov 18 '18 at 16:31











  • twoDim was defined with int twoDim = new int[numOfVals];. numOfVals is just an integer user input. I'll fix the scoping, thank you for point out that j is outside of it's loop in the second mention of it. I think i copy-pasted but forgot to change it to k[m] instead of k[j]

    – Zacki Aseer
    Nov 18 '18 at 16:39

















1. j's scope is limited only to its for-loop. 2. How have you defined twoDim?

– Nicholas K
Nov 18 '18 at 16:31





1. j's scope is limited only to its for-loop. 2. How have you defined twoDim?

– Nicholas K
Nov 18 '18 at 16:31













twoDim was defined with int twoDim = new int[numOfVals];. numOfVals is just an integer user input. I'll fix the scoping, thank you for point out that j is outside of it's loop in the second mention of it. I think i copy-pasted but forgot to change it to k[m] instead of k[j]

– Zacki Aseer
Nov 18 '18 at 16:39





twoDim was defined with int twoDim = new int[numOfVals];. numOfVals is just an integer user input. I'll fix the scoping, thank you for point out that j is outside of it's loop in the second mention of it. I think i copy-pasted but forgot to change it to k[m] instead of k[j]

– Zacki Aseer
Nov 18 '18 at 16:39












2 Answers
2






active

oldest

votes


















0














The issues are :





  1. j's scope is limited only to its for-loop. You can't access it outside the loop.


  2. twoDim[k[j]]=1; and twoDim[k[j]]=0. k is of type int, you can't try to use it as an array.






share|improve this answer































    0














    Try this



    for (int i = 0; i < numOfVals; i++){
    numSpc = 50 - values[i];
    for (int k = 0; k < 51; k++){
    twoDim[k]=;
    for (int j = 0; j < values[i]; j++){
    twoDim[k].push(1);
    }
    for (int m = 0; m < numSpc; m ++){
    twoDim[k].push(0);
    }
    }
    }





    share|improve this answer























      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',
      autoActivateHeartbeat: false,
      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%2f53363065%2fcreating-a-2-dimensional-array-with-loops%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The issues are :





      1. j's scope is limited only to its for-loop. You can't access it outside the loop.


      2. twoDim[k[j]]=1; and twoDim[k[j]]=0. k is of type int, you can't try to use it as an array.






      share|improve this answer




























        0














        The issues are :





        1. j's scope is limited only to its for-loop. You can't access it outside the loop.


        2. twoDim[k[j]]=1; and twoDim[k[j]]=0. k is of type int, you can't try to use it as an array.






        share|improve this answer


























          0












          0








          0







          The issues are :





          1. j's scope is limited only to its for-loop. You can't access it outside the loop.


          2. twoDim[k[j]]=1; and twoDim[k[j]]=0. k is of type int, you can't try to use it as an array.






          share|improve this answer













          The issues are :





          1. j's scope is limited only to its for-loop. You can't access it outside the loop.


          2. twoDim[k[j]]=1; and twoDim[k[j]]=0. k is of type int, you can't try to use it as an array.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 16:42









          Nicholas KNicholas K

          6,90761133




          6,90761133

























              0














              Try this



              for (int i = 0; i < numOfVals; i++){
              numSpc = 50 - values[i];
              for (int k = 0; k < 51; k++){
              twoDim[k]=;
              for (int j = 0; j < values[i]; j++){
              twoDim[k].push(1);
              }
              for (int m = 0; m < numSpc; m ++){
              twoDim[k].push(0);
              }
              }
              }





              share|improve this answer




























                0














                Try this



                for (int i = 0; i < numOfVals; i++){
                numSpc = 50 - values[i];
                for (int k = 0; k < 51; k++){
                twoDim[k]=;
                for (int j = 0; j < values[i]; j++){
                twoDim[k].push(1);
                }
                for (int m = 0; m < numSpc; m ++){
                twoDim[k].push(0);
                }
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  Try this



                  for (int i = 0; i < numOfVals; i++){
                  numSpc = 50 - values[i];
                  for (int k = 0; k < 51; k++){
                  twoDim[k]=;
                  for (int j = 0; j < values[i]; j++){
                  twoDim[k].push(1);
                  }
                  for (int m = 0; m < numSpc; m ++){
                  twoDim[k].push(0);
                  }
                  }
                  }





                  share|improve this answer













                  Try this



                  for (int i = 0; i < numOfVals; i++){
                  numSpc = 50 - values[i];
                  for (int k = 0; k < 51; k++){
                  twoDim[k]=;
                  for (int j = 0; j < values[i]; j++){
                  twoDim[k].push(1);
                  }
                  for (int m = 0; m < numSpc; m ++){
                  twoDim[k].push(0);
                  }
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 '18 at 16:51









                  Abdul AleemAbdul Aleem

                  12




                  12






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363065%2fcreating-a-2-dimensional-array-with-loops%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







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()