How to save integers from map to using the for loop
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
add a comment |
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
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
add a comment |
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
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
java arrays dictionary
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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;
}
Thank you for explaining this to me.
– Reddi
Jan 2 at 17:13
add a comment |
You can use streams:
int arrayOfIntegers = fooMap.keySet().stream()
.mapToInt(k->k).toArray();
add a comment |
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));
}
}
add a comment |
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
});
}
});
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%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
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;
}
Thank you for explaining this to me.
– Reddi
Jan 2 at 17:13
add a comment |
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;
}
Thank you for explaining this to me.
– Reddi
Jan 2 at 17:13
add a comment |
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;
}
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;
}
answered Jan 2 at 17:09
MureinikMureinik
185k22137203
185k22137203
Thank you for explaining this to me.
– Reddi
Jan 2 at 17:13
add a comment |
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
add a comment |
You can use streams:
int arrayOfIntegers = fooMap.keySet().stream()
.mapToInt(k->k).toArray();
add a comment |
You can use streams:
int arrayOfIntegers = fooMap.keySet().stream()
.mapToInt(k->k).toArray();
add a comment |
You can use streams:
int arrayOfIntegers = fooMap.keySet().stream()
.mapToInt(k->k).toArray();
You can use streams:
int arrayOfIntegers = fooMap.keySet().stream()
.mapToInt(k->k).toArray();
answered Jan 2 at 17:25
MorpheusMorpheus
1736
1736
add a comment |
add a comment |
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));
}
}
add a comment |
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));
}
}
add a comment |
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));
}
}
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));
}
}
answered Jan 2 at 18:10
AshokAshok
125
125
add a comment |
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.
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%2f54010351%2fhow-to-save-integers-from-map-to-using-the-for-loop%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
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