Re: "final" in "for"
Lew wrote :
Wojtek wrote:
If this does come about, final is fine.
Though one could STILL monkey about:
int inc = 1;
for (final int i = 0; i < s.length(); i += inc)
{
? inc = 1;
? char ch = s.charAt(i);
? if (ch != '\')
? {
? ? out.write(ch);
? }
? else
? {
? ? out.write(mapEscapeChar(s.charAt(i + 1)));
? ? inc = 2;
? }
}
The question arises whether 'final' would apply to all declared loop
variables:
for ( int inc = 1, i = 0; i < s.length(); i += inc)
vs.
for ( final int inc = 1, i = 0; i < s.length(); i += inc)
Ah, the joys of working around language limitations:
private enum Step
{
SINGLE(1),
TWO(2),
THREE(3);
private int distance;
Step(int distanceValue)
{
distance = distanceValue;
}
private int getDistance()
{
return distance;
}
}
Step step = Step.SINGLE;
for (final int i = 0; i < s.length(); i += step.getDistance() )
{
? step = Step.SINGLE;
? char ch = s.charAt(i);
? if (ch != '\')
? {
? ? out.write(ch);
? }
? else
? {
? ? out.write(mapEscapeChar(s.charAt(i + 1)));
? ? step = Step.TWO;
? }
}
and "out.write(mapEscapeChar(s.charAt(i + 1)));" should be wrapped in a
try/catch in case the '\' is the last character.
--
Wojtek :-)