> This one seems alright to look at but gives me an
> ArrayIndexOutOfBoundsException. I can't see where I am crossing the
> array bounds.
> for ( int i = 0; i < args.length; i++, sb.append( args[i] ),
If i was args.length-1, incrementing takes it to args.length whcih you
then use. Remember, the third part of the for loop is executed after the
body, so moving stuff onto the end of it rearranges the order.
> sb.append(" "));
Your code is the equivalent of:
{
int i = 0;
while (i < args.length) {
i++;
sb.append(args[i]);
sb.append(" ");
}
}
Tom Hawtin
Sathyaish - 05 Apr 2007 17:25 GMT
Oh, thank you, Tom!
:-)
You made my day. What a profound piece of learning. It is a wonder
that we learn from the smallest of examples, the most valuable
lessons. I've been programming for several years but only just
discovered this. That was because I never wrote such a large for loop
statement in commercial code. In practice sessions, though, one learns
the most.
Thank you once again.
Lew - 05 Apr 2007 22:55 GMT
> Oh, thank you, Tom!
>
[quoted text clipped - 8 lines]
>
> Thank you once again.
You avoid this issue if you put the loop body in, well, the loop body instead
of the header.

Signature
Lew