“Auto increment” alphabet in Java?

后端 未结 10 2097
情书的邮戳
情书的邮戳 2020-12-02 16:53

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

相关标签:
10条回答
  • 2020-12-02 17:22

    Mandatory Java 8 solution:

    IntStream.rangeClosed('A', 'Z')
             .mapToObj(c -> "" + (char) c)
             .forEach(System.out::println);
    
    0 讨论(0)
  • 2020-12-02 17:26

    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.

    0 讨论(0)
  • 2020-12-02 17:27

    Yes, like this:

    for (int i = 0; i < 26; i++)
    {
        char upper = (char) ('A' + i);
        char lower = (char) ('a' + i);
        ...
    }
    
    0 讨论(0)
  • 2020-12-02 17:29
    for (char c = 'A'; c <= 'Z'; c++) {
      ...
    }
    
    0 讨论(0)
  • 2020-12-02 17:32

    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);
    }
    
    0 讨论(0)
  • 2020-12-02 17:35

    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

    0 讨论(0)
提交回复
热议问题