Creating a 2 dimensional array with loops
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
add a comment |
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
1.j
's scope is limited only to itsfor-loop
. 2. How have you definedtwoDim
?
– Nicholas K
Nov 18 '18 at 16:31
twoDim
was defined withint 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 tok[m]
instead ofk[j]
– Zacki Aseer
Nov 18 '18 at 16:39
add a comment |
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
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
java arrays
asked Nov 18 '18 at 16:28
Zacki AseerZacki Aseer
62
62
1.j
's scope is limited only to itsfor-loop
. 2. How have you definedtwoDim
?
– Nicholas K
Nov 18 '18 at 16:31
twoDim
was defined withint 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 tok[m]
instead ofk[j]
– Zacki Aseer
Nov 18 '18 at 16:39
add a comment |
1.j
's scope is limited only to itsfor-loop
. 2. How have you definedtwoDim
?
– Nicholas K
Nov 18 '18 at 16:31
twoDim
was defined withint 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 tok[m]
instead ofk[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
add a comment |
2 Answers
2
active
oldest
votes
The issues are :
j
's scope is limited only to itsfor-loop
. You can't access it outside the loop.
twoDim[k[j]]=1;
andtwoDim[k[j]]=0
. k is of typeint
, you can't try to use it as an array.
add a comment |
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);
}
}
}
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%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
The issues are :
j
's scope is limited only to itsfor-loop
. You can't access it outside the loop.
twoDim[k[j]]=1;
andtwoDim[k[j]]=0
. k is of typeint
, you can't try to use it as an array.
add a comment |
The issues are :
j
's scope is limited only to itsfor-loop
. You can't access it outside the loop.
twoDim[k[j]]=1;
andtwoDim[k[j]]=0
. k is of typeint
, you can't try to use it as an array.
add a comment |
The issues are :
j
's scope is limited only to itsfor-loop
. You can't access it outside the loop.
twoDim[k[j]]=1;
andtwoDim[k[j]]=0
. k is of typeint
, you can't try to use it as an array.
The issues are :
j
's scope is limited only to itsfor-loop
. You can't access it outside the loop.
twoDim[k[j]]=1;
andtwoDim[k[j]]=0
. k is of typeint
, you can't try to use it as an array.
answered Nov 18 '18 at 16:42
Nicholas KNicholas K
6,90761133
6,90761133
add a comment |
add a comment |
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);
}
}
}
add a comment |
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);
}
}
}
add a comment |
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);
}
}
}
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);
}
}
}
answered Nov 18 '18 at 16:51
Abdul AleemAbdul Aleem
12
12
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%2f53363065%2fcreating-a-2-dimensional-array-with-loops%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.
j
's scope is limited only to itsfor-loop
. 2. How have you definedtwoDim
?– Nicholas K
Nov 18 '18 at 16:31
twoDim
was defined withint 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 tok[m]
instead ofk[j]
– Zacki Aseer
Nov 18 '18 at 16:39