How to save integers from map to using the for loop












0















I have a question to you, I am trying to save integers from map to an array using for loop. This example below is not working as I wanted, because when I display that array of ints it only have '2' for 10 elements, but I wanted to get [1,2,0,0,0,0...], what should be changed in that code?



Map<Integer, String> fooMap = new HashMap<>();
fooMap.put(1, "AB");
fooMap.put(2, "BBA");

int arrayOfIntegers = new int[10];

for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
int val = values.getKey();
System.out.println(val);
for (int index = 0; index < arrayOfIntegers.length; index++) {
arrayOfIntegers[index] = val;
}
}









share|improve this question




















  • 1





    You loop around all the entries (1 -> AB and 2 -> BBA), print the key (1 and 2) and then write that key to every index of an array that is never read. What's confusing you about it?

    – Michael
    Jan 2 at 17:07








  • 1





    For each element in the map, you're iterating over the entire array and setting each index in it to the value.

    – hnefatl
    Jan 2 at 17:07
















0















I have a question to you, I am trying to save integers from map to an array using for loop. This example below is not working as I wanted, because when I display that array of ints it only have '2' for 10 elements, but I wanted to get [1,2,0,0,0,0...], what should be changed in that code?



Map<Integer, String> fooMap = new HashMap<>();
fooMap.put(1, "AB");
fooMap.put(2, "BBA");

int arrayOfIntegers = new int[10];

for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
int val = values.getKey();
System.out.println(val);
for (int index = 0; index < arrayOfIntegers.length; index++) {
arrayOfIntegers[index] = val;
}
}









share|improve this question




















  • 1





    You loop around all the entries (1 -> AB and 2 -> BBA), print the key (1 and 2) and then write that key to every index of an array that is never read. What's confusing you about it?

    – Michael
    Jan 2 at 17:07








  • 1





    For each element in the map, you're iterating over the entire array and setting each index in it to the value.

    – hnefatl
    Jan 2 at 17:07














0












0








0








I have a question to you, I am trying to save integers from map to an array using for loop. This example below is not working as I wanted, because when I display that array of ints it only have '2' for 10 elements, but I wanted to get [1,2,0,0,0,0...], what should be changed in that code?



Map<Integer, String> fooMap = new HashMap<>();
fooMap.put(1, "AB");
fooMap.put(2, "BBA");

int arrayOfIntegers = new int[10];

for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
int val = values.getKey();
System.out.println(val);
for (int index = 0; index < arrayOfIntegers.length; index++) {
arrayOfIntegers[index] = val;
}
}









share|improve this question
















I have a question to you, I am trying to save integers from map to an array using for loop. This example below is not working as I wanted, because when I display that array of ints it only have '2' for 10 elements, but I wanted to get [1,2,0,0,0,0...], what should be changed in that code?



Map<Integer, String> fooMap = new HashMap<>();
fooMap.put(1, "AB");
fooMap.put(2, "BBA");

int arrayOfIntegers = new int[10];

for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
int val = values.getKey();
System.out.println(val);
for (int index = 0; index < arrayOfIntegers.length; index++) {
arrayOfIntegers[index] = val;
}
}






java arrays dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 17:15









Mureinik

185k22137203




185k22137203










asked Jan 2 at 17:04









ReddiReddi

168110




168110








  • 1





    You loop around all the entries (1 -> AB and 2 -> BBA), print the key (1 and 2) and then write that key to every index of an array that is never read. What's confusing you about it?

    – Michael
    Jan 2 at 17:07








  • 1





    For each element in the map, you're iterating over the entire array and setting each index in it to the value.

    – hnefatl
    Jan 2 at 17:07














  • 1





    You loop around all the entries (1 -> AB and 2 -> BBA), print the key (1 and 2) and then write that key to every index of an array that is never read. What's confusing you about it?

    – Michael
    Jan 2 at 17:07








  • 1





    For each element in the map, you're iterating over the entire array and setting each index in it to the value.

    – hnefatl
    Jan 2 at 17:07








1




1





You loop around all the entries (1 -> AB and 2 -> BBA), print the key (1 and 2) and then write that key to every index of an array that is never read. What's confusing you about it?

– Michael
Jan 2 at 17:07







You loop around all the entries (1 -> AB and 2 -> BBA), print the key (1 and 2) and then write that key to every index of an array that is never read. What's confusing you about it?

– Michael
Jan 2 at 17:07






1




1





For each element in the map, you're iterating over the entire array and setting each index in it to the value.

– hnefatl
Jan 2 at 17:07





For each element in the map, you're iterating over the entire array and setting each index in it to the value.

– hnefatl
Jan 2 at 17:07












3 Answers
3






active

oldest

votes


















1














In each iteration of the loop you overwrite the entire array. You could instead save the array's index outside the loop and use it to update the array:



int index = 0;
for (Integer val: fooMap.keySet()) {
arrayOfIntegers[index] = val;
++index;
}





share|improve this answer
























  • Thank you for explaining this to me.

    – Reddi
    Jan 2 at 17:13



















0














You can use streams:



int arrayOfIntegers = fooMap.keySet().stream()
.mapToInt(k->k).toArray();





share|improve this answer































    0














    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;

    public class stack1 {
    public static void main(String args) {
    Map<Integer, String> fooMap = new HashMap<>();
    fooMap.put(1, "AB");
    fooMap.put(2, "BBA");

    int memoryAllocated = 10;
    int arrayOfIntegers = new int[memoryAllocated];
    int pos =0;

    for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
    int val = values.getKey();
    arrayOfIntegers[pos]=val;
    pos =pos+1;
    }

    while(pos < memoryAllocated){
    arrayOfIntegers[pos]=0;
    pos = pos+1;
    }

    System.out.println("Arrays : "+Arrays.toString(arrayOfIntegers));
    }
    }





    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%2f54010351%2fhow-to-save-integers-from-map-to-using-the-for-loop%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      In each iteration of the loop you overwrite the entire array. You could instead save the array's index outside the loop and use it to update the array:



      int index = 0;
      for (Integer val: fooMap.keySet()) {
      arrayOfIntegers[index] = val;
      ++index;
      }





      share|improve this answer
























      • Thank you for explaining this to me.

        – Reddi
        Jan 2 at 17:13
















      1














      In each iteration of the loop you overwrite the entire array. You could instead save the array's index outside the loop and use it to update the array:



      int index = 0;
      for (Integer val: fooMap.keySet()) {
      arrayOfIntegers[index] = val;
      ++index;
      }





      share|improve this answer
























      • Thank you for explaining this to me.

        – Reddi
        Jan 2 at 17:13














      1












      1








      1







      In each iteration of the loop you overwrite the entire array. You could instead save the array's index outside the loop and use it to update the array:



      int index = 0;
      for (Integer val: fooMap.keySet()) {
      arrayOfIntegers[index] = val;
      ++index;
      }





      share|improve this answer













      In each iteration of the loop you overwrite the entire array. You could instead save the array's index outside the loop and use it to update the array:



      int index = 0;
      for (Integer val: fooMap.keySet()) {
      arrayOfIntegers[index] = val;
      ++index;
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 2 at 17:09









      MureinikMureinik

      185k22137203




      185k22137203













      • Thank you for explaining this to me.

        – Reddi
        Jan 2 at 17:13



















      • Thank you for explaining this to me.

        – Reddi
        Jan 2 at 17:13

















      Thank you for explaining this to me.

      – Reddi
      Jan 2 at 17:13





      Thank you for explaining this to me.

      – Reddi
      Jan 2 at 17:13













      0














      You can use streams:



      int arrayOfIntegers = fooMap.keySet().stream()
      .mapToInt(k->k).toArray();





      share|improve this answer




























        0














        You can use streams:



        int arrayOfIntegers = fooMap.keySet().stream()
        .mapToInt(k->k).toArray();





        share|improve this answer


























          0












          0








          0







          You can use streams:



          int arrayOfIntegers = fooMap.keySet().stream()
          .mapToInt(k->k).toArray();





          share|improve this answer













          You can use streams:



          int arrayOfIntegers = fooMap.keySet().stream()
          .mapToInt(k->k).toArray();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 17:25









          MorpheusMorpheus

          1736




          1736























              0














              import java.util.Arrays;
              import java.util.HashMap;
              import java.util.Map;

              public class stack1 {
              public static void main(String args) {
              Map<Integer, String> fooMap = new HashMap<>();
              fooMap.put(1, "AB");
              fooMap.put(2, "BBA");

              int memoryAllocated = 10;
              int arrayOfIntegers = new int[memoryAllocated];
              int pos =0;

              for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
              int val = values.getKey();
              arrayOfIntegers[pos]=val;
              pos =pos+1;
              }

              while(pos < memoryAllocated){
              arrayOfIntegers[pos]=0;
              pos = pos+1;
              }

              System.out.println("Arrays : "+Arrays.toString(arrayOfIntegers));
              }
              }





              share|improve this answer




























                0














                import java.util.Arrays;
                import java.util.HashMap;
                import java.util.Map;

                public class stack1 {
                public static void main(String args) {
                Map<Integer, String> fooMap = new HashMap<>();
                fooMap.put(1, "AB");
                fooMap.put(2, "BBA");

                int memoryAllocated = 10;
                int arrayOfIntegers = new int[memoryAllocated];
                int pos =0;

                for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
                int val = values.getKey();
                arrayOfIntegers[pos]=val;
                pos =pos+1;
                }

                while(pos < memoryAllocated){
                arrayOfIntegers[pos]=0;
                pos = pos+1;
                }

                System.out.println("Arrays : "+Arrays.toString(arrayOfIntegers));
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  import java.util.Arrays;
                  import java.util.HashMap;
                  import java.util.Map;

                  public class stack1 {
                  public static void main(String args) {
                  Map<Integer, String> fooMap = new HashMap<>();
                  fooMap.put(1, "AB");
                  fooMap.put(2, "BBA");

                  int memoryAllocated = 10;
                  int arrayOfIntegers = new int[memoryAllocated];
                  int pos =0;

                  for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
                  int val = values.getKey();
                  arrayOfIntegers[pos]=val;
                  pos =pos+1;
                  }

                  while(pos < memoryAllocated){
                  arrayOfIntegers[pos]=0;
                  pos = pos+1;
                  }

                  System.out.println("Arrays : "+Arrays.toString(arrayOfIntegers));
                  }
                  }





                  share|improve this answer













                  import java.util.Arrays;
                  import java.util.HashMap;
                  import java.util.Map;

                  public class stack1 {
                  public static void main(String args) {
                  Map<Integer, String> fooMap = new HashMap<>();
                  fooMap.put(1, "AB");
                  fooMap.put(2, "BBA");

                  int memoryAllocated = 10;
                  int arrayOfIntegers = new int[memoryAllocated];
                  int pos =0;

                  for (Map.Entry<Integer, String> values : fooMap.entrySet()) {
                  int val = values.getKey();
                  arrayOfIntegers[pos]=val;
                  pos =pos+1;
                  }

                  while(pos < memoryAllocated){
                  arrayOfIntegers[pos]=0;
                  pos = pos+1;
                  }

                  System.out.println("Arrays : "+Arrays.toString(arrayOfIntegers));
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 18:10









                  AshokAshok

                  125




                  125






























                      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%2f54010351%2fhow-to-save-integers-from-map-to-using-the-for-loop%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas