I have two for loops like the below..the second not shown is the adder:
I pass verification, but code style complains: "control variable i is modified"
Any suggestions? I looked at other questions in help and it seems others are doing something similar.
Maybe try a while loop?
int originalSize = list.size();
for (int i = 0; i < originalSize; i++) {
if (list.get(i).contains("r") && !list.get(i).contains("l")) {
//System.out.println("string contains r but not l: " + list.get(i));
list.remove(i);
i--;
originalSize--;
}
}
Kent Hervey
Level 16
Verification works, but code style fails
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
16 November 2019, 04:10
code style doesn't fail. This is more of a suggestion to implore you to have better code habits, but it is deeply flawed. The best thing you can do is name your variables meaningfully, and work at shortening your code.
0
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
19 November 2019, 04:09
the error was about modifying the counter, not my naming..so help me...
0
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
16 November 2019, 00:54
I really don't get it, but...
https://stackoverflow.com/questions/42519238/why-is-it-bad-to-change-for-loop-counter-in-java
0
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
16 November 2019, 00:51
Using while loops did make code style happy.
0