Re: increment letters
Twisted wrote:
On Jul 31, 8:13 pm, JackT <jackt...@gmail.com> wrote:
You can even use Objects. For example, before Java 5,
the standard iterator pattern is this:
[snip]
Yes, of course I'm familiar with that. I just seem to recall it
balking on most primitive types other than "int", or at least throwing
off a torrent of warnings. C compilers usually also warn, at least on
use of char as a loop index. Which was exactly what was suggested
here.
And I'm fairly sure Java does choke on just about anything but "int"
for an array index, although that's moot if you don't use the loop
counter as an array index or do cast it to int. It definitely doesn't
permit longs as array indices.
I agree about longs as array indices, because the array index has to be
convertible to int by unary numeric promotion, but char is fine.
public class CharAsLoopIndex {
public static void main(String[] args) {
String[] array = new String[5];
for(char c = 0; c < array.length; c++){
array[c] = String.valueOf((char)(c+'A'));
}
}
}
Remember Java does not have a concept of a loop counter as a language
structure.
Patricia