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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2047228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:58:47  来源:igfitidea点击:

"Auto increment" alphabet in Java?

javaauto-increment

提问by Dacto

"Auto increment" alphabet in Java - is this possible? From A to Z without a third-party library?

Java中的“自动增量”字母表 - 这可能吗?从 A 到 Z 没有第三方库?

采纳答案by Richie

Yes, you can do it like this:

是的,你可以这样做:

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

It is also possible with typecasting:

也可以通过类型转换:

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

回答by Taylor Leese

Yes, like this:

是的,像这样:

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

回答by sdornan

for (char c = 'a'; c <= 'z'; c++)
  //whatever

回答by Laurence Gonsalves

for (char c = 'A'; c <= 'Z'; c++) {
  ...
}

回答by Michel Gokan

You are looking for something like this:

你正在寻找这样的东西:

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

回答by raja

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

Use this for lowercase alphabets.

将此用于小写字母。

回答by EngineerCoding

Here is a piece of code to see what really is going on (or at least the print out :P):

这是一段代码,用于查看真正发生了什么(或至少打印出来:P):

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

回答by Massimiliano Giunchi

This is my solutions, just a little more complicated than other examples above, but extendible for other iterations (used pattern iterator):

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

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+" ");
        }
    }
}

Output: B C D E F G H I J K L M N O P Q R S T U V W X Y

输出: BCDEFGHIJKLMNOPQRSTUV WXY

回答by Lukas Eder

Mandatory Java 8 solution:

强制 Java 8 解决方案:

IntStream.rangeClosed('A', 'Z')
         .mapToObj(c -> "" + (char) c)
         .forEach(System.out::println);

回答by Shiva

Here's the code without a third-party library,

这是没有第三方库的代码,

public class JavaPrintAlphabet
{
   public static void main(String[] args)
   {
      System.out.println("Printing the alphabets from A to Z : ");
      char alpha;

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

Output

输出

Printing the alphabets from A to Z : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

For more on a Java program to print the alphabet, refer this resource.

有关打印字母表的 Java 程序的更多信息,请参阅此资源