How do I check if a position in my vector of vectors is out of bounds?
up vote
0
down vote
favorite
I have a vector of vectors filled with characters from a text file. It is essentially a simple outbreak simulator, with 'i' characters being infected, and 's' characters being susceptible to infection. The point is to run through the matrix and if it comes across an 'i', it then changes all 's' around it into an 'i'. I run into a problem when checking the elements around it due to checking positions out of the bounds on the edges of the matrix. Is there a way to check these bounds in my if statements?
Here is the code:
for (int i = 0; i < population.size(); i++)
for(int j = 0; j < population[i].size(); j++)
if(population[i][j] == 'i')
if(population[i-1][j] == 's')
population[i-1][j] = 'i';
if(population[i-1][j+1] == 's')
population[i-1][j+1] = 'i';
if(population[i][j+1] == 's')
population[i][j+1] = 'i';
if(population[i+1][j+1] == 's')
population[i+1][j+1] = 'i';
if(population[i+1][j] == 's')
population[i+1][j] = 'i';
if(population[i+1][j-1] == 's')
population[i+1][j-1] = 'i';
if(population[i][j-1] == 's')
population[i][j-1] = 'i';
java matrix vector indexoutofboundsexception
add a comment |
up vote
0
down vote
favorite
I have a vector of vectors filled with characters from a text file. It is essentially a simple outbreak simulator, with 'i' characters being infected, and 's' characters being susceptible to infection. The point is to run through the matrix and if it comes across an 'i', it then changes all 's' around it into an 'i'. I run into a problem when checking the elements around it due to checking positions out of the bounds on the edges of the matrix. Is there a way to check these bounds in my if statements?
Here is the code:
for (int i = 0; i < population.size(); i++)
for(int j = 0; j < population[i].size(); j++)
if(population[i][j] == 'i')
if(population[i-1][j] == 's')
population[i-1][j] = 'i';
if(population[i-1][j+1] == 's')
population[i-1][j+1] = 'i';
if(population[i][j+1] == 's')
population[i][j+1] = 'i';
if(population[i+1][j+1] == 's')
population[i+1][j+1] = 'i';
if(population[i+1][j] == 's')
population[i+1][j] = 'i';
if(population[i+1][j-1] == 's')
population[i+1][j-1] = 'i';
if(population[i][j-1] == 's')
population[i][j-1] = 'i';
java matrix vector indexoutofboundsexception
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a vector of vectors filled with characters from a text file. It is essentially a simple outbreak simulator, with 'i' characters being infected, and 's' characters being susceptible to infection. The point is to run through the matrix and if it comes across an 'i', it then changes all 's' around it into an 'i'. I run into a problem when checking the elements around it due to checking positions out of the bounds on the edges of the matrix. Is there a way to check these bounds in my if statements?
Here is the code:
for (int i = 0; i < population.size(); i++)
for(int j = 0; j < population[i].size(); j++)
if(population[i][j] == 'i')
if(population[i-1][j] == 's')
population[i-1][j] = 'i';
if(population[i-1][j+1] == 's')
population[i-1][j+1] = 'i';
if(population[i][j+1] == 's')
population[i][j+1] = 'i';
if(population[i+1][j+1] == 's')
population[i+1][j+1] = 'i';
if(population[i+1][j] == 's')
population[i+1][j] = 'i';
if(population[i+1][j-1] == 's')
population[i+1][j-1] = 'i';
if(population[i][j-1] == 's')
population[i][j-1] = 'i';
java matrix vector indexoutofboundsexception
I have a vector of vectors filled with characters from a text file. It is essentially a simple outbreak simulator, with 'i' characters being infected, and 's' characters being susceptible to infection. The point is to run through the matrix and if it comes across an 'i', it then changes all 's' around it into an 'i'. I run into a problem when checking the elements around it due to checking positions out of the bounds on the edges of the matrix. Is there a way to check these bounds in my if statements?
Here is the code:
for (int i = 0; i < population.size(); i++)
for(int j = 0; j < population[i].size(); j++)
if(population[i][j] == 'i')
if(population[i-1][j] == 's')
population[i-1][j] = 'i';
if(population[i-1][j+1] == 's')
population[i-1][j+1] = 'i';
if(population[i][j+1] == 's')
population[i][j+1] = 'i';
if(population[i+1][j+1] == 's')
population[i+1][j+1] = 'i';
if(population[i+1][j] == 's')
population[i+1][j] = 'i';
if(population[i+1][j-1] == 's')
population[i+1][j-1] = 'i';
if(population[i][j-1] == 's')
population[i][j-1] = 'i';
java matrix vector indexoutofboundsexception
java matrix vector indexoutofboundsexception
edited Nov 10 at 3:58
mjuarez
9,36973651
9,36973651
asked Nov 10 at 3:42
Bryan Murray
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Instead of directly referencing a particular array entry, you could do something like the following:
void checkForInfectionAndInfectIfNeeded(int i, int j)
for (int row = -1; row <= 1; row++)
for (int column = -1; column <=1; column++)
infect(i + row, j + column);
void infect(int i, int j) j >= population[j].size())
return;
else
population[i][j] = 'i';
This way, the infect
method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.
Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
– Bryan Murray
Nov 10 at 5:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Instead of directly referencing a particular array entry, you could do something like the following:
void checkForInfectionAndInfectIfNeeded(int i, int j)
for (int row = -1; row <= 1; row++)
for (int column = -1; column <=1; column++)
infect(i + row, j + column);
void infect(int i, int j) j >= population[j].size())
return;
else
population[i][j] = 'i';
This way, the infect
method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.
Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
– Bryan Murray
Nov 10 at 5:29
add a comment |
up vote
1
down vote
Instead of directly referencing a particular array entry, you could do something like the following:
void checkForInfectionAndInfectIfNeeded(int i, int j)
for (int row = -1; row <= 1; row++)
for (int column = -1; column <=1; column++)
infect(i + row, j + column);
void infect(int i, int j) j >= population[j].size())
return;
else
population[i][j] = 'i';
This way, the infect
method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.
Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
– Bryan Murray
Nov 10 at 5:29
add a comment |
up vote
1
down vote
up vote
1
down vote
Instead of directly referencing a particular array entry, you could do something like the following:
void checkForInfectionAndInfectIfNeeded(int i, int j)
for (int row = -1; row <= 1; row++)
for (int column = -1; column <=1; column++)
infect(i + row, j + column);
void infect(int i, int j) j >= population[j].size())
return;
else
population[i][j] = 'i';
This way, the infect
method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.
Instead of directly referencing a particular array entry, you could do something like the following:
void checkForInfectionAndInfectIfNeeded(int i, int j)
for (int row = -1; row <= 1; row++)
for (int column = -1; column <=1; column++)
infect(i + row, j + column);
void infect(int i, int j) j >= population[j].size())
return;
else
population[i][j] = 'i';
This way, the infect
method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.
answered Nov 10 at 3:58
mjuarez
9,36973651
9,36973651
Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
– Bryan Murray
Nov 10 at 5:29
add a comment |
Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
– Bryan Murray
Nov 10 at 5:29
Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
– Bryan Murray
Nov 10 at 5:29
Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
– Bryan Murray
Nov 10 at 5:29
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.
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.
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%2f53235818%2fhow-do-i-check-if-a-position-in-my-vector-of-vectors-is-out-of-bounds%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