在Java中的“自动增量”字母表?

在Java中的“自动增量”字母表 – 这是可能的吗? 从A到Z没有第三方图书馆?

是的,你可以这样做:

for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) { System.out.println(alphabet); } 

types转换也是可能的:

 for (int i = 65; i <= 90; i++) { System.out.println((char)i); } 

是的,像这样:

 for (int i = 0; i < 26; i++) { char upper = (char) ('A' + i); char lower = (char) ('a' + i); ... } 
 for (char c = 'A'; c <= 'Z'; c++) { ... } 

你正在寻找这样的东西:

  for( int i = 'a'; i < 'z'; i++ ) System.out.println((char)i);//cast int to char 

强制性的Java 8解决scheme:

 IntStream.rangeClosed('A', 'Z') .mapToObj(c -> "" + (char) c) .forEach(System.out::println); 
 for (char c = 'a'; c <= 'z'; c++) //whatever 

这是我的解决scheme,只比上面的其他示例复杂一点,但可以扩展用于其他迭代(使用模式迭代器):

 class Alphabet implements Iterable<String>{ private char start; private char end; public Alphabet(char start, char end) { this.start=start; this.end=end; } @Override public Iterator<String> iterator() { return new AlphabetIterator(start, end); } class AlphabetIterator implements Iterator<String>{ private String current; private String end; private AlphabetIterator(char start, char end) { this.current=String.valueOf(--start); this.end=String.valueOf(end); } @Override public boolean hasNext() { return (current.charAt(0) < end.charAt(0)); } @Override public String next() { char nextChar = current.charAt(0); return this.current=String.valueOf(++nextChar); } } public static void main (String[] arg){ for (String str:new Alphabet('B', 'Y')){ System.out.print(str+" "); } } } 

输出:BCDEFGHIJKLMNOPQRSTUV WXY

  for(char alphabet = 'a'; alphabet <= 'z';alphabet++){ System.out.println(alphabet); } 

使用这个小写字母

这里是一段代码,看看究竟是怎么回事(或者至less是打印输出:P):

 for(int i = 0; i < 26; i++) { System.out.println((char)('A' + i) + ":" + ('A' + i) + " : " + (char)('a' + i) + ":" + ('z' + i)); }