Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unused variable in a loop

I need to build a pattern string according to an argument list. If the arguments are "foo", "bar", "data", then pattern should be: "?, ?, ?"

My code is:

List<String> args;
...
for(String s : args) {
  pattern += "?,";
}
pattern = pattern.substring(0, pattern.length()-1);

It works fine, the only concern is, s is not used, it seems the code is a little dirty.

Any improvements for this?

I hope something like:

for(args.size()) {
    ...
}

But apparently there isn't..

like image 895
Deqing Avatar asked Dec 09 '25 23:12

Deqing


1 Answers

You could use the class for loop with conditions:

for (int i = 0, s < args.size(); i++)

In this case, i is being used as a counting variable.

Other than that, there aren't any improvements to be made, although there isn't a need for improvements.

like image 54
Michael Yaworski Avatar answered Dec 12 '25 13:12

Michael Yaworski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!