Better way to generate array of all letters in the alphabet

后端 未结 17 1923
说谎
说谎 2020-12-02 10:56

Right now I\'m doing

for (char c = \'a\'; c <= \'z\'; c++) {
    alphabet[c - \'a\'] = c;
}

but is there a better way to do it? Similar

相关标签:
17条回答
  • 2020-12-02 11:29

    Define any languages an enum, and call getAlphabet();

    char[] armenianAlphabet = getAlphabet(LocaleLanguage.ARMENIAN);
    char[] russianAlphabet = getAlphabet(LocaleLanguage.RUSSIAN);
    
    // get uppercase alphabet 
    char[] currentAlphabet = getAlphabet(true);
        
    System.out.println(armenianAlphabet);
    System.out.println(russianAlphabet);
    System.out.println(currentAlphabet);
    

    result

    I/System.out: աբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆ

    I/System.out: абвгдежзийклмнопрстуфхцчшщъыьэюя

    I/System.out: ABCDEFGHIJKLMNOPQRSTUVWXYZ

    private char[] getAlphabet(){
        return getAlphabet(false);
    }
    
    private char[] getAlphabet(boolean flagToUpperCase){
        Locale locale = getResources().getConfiguration().locale;
        LocaleLanguage language = LocaleLanguage.getLocalLanguage(locale);
        return getAlphabet(language, flagToUpperCase);
    }
    
    private char[] getAlphabet(LocaleLanguage localeLanguage, boolean flagToUpperCase){
        if (localeLanguage == null)
            localeLanguage = LocaleLanguage.ENGLISH;
    
        char firstLetter = localeLanguage.getFirstLetter();
        char lastLetter = localeLanguage.getLastLetter();
        int alphabetSize = lastLetter - firstLetter + 1;
    
        char[] alphabet = new char[alphabetSize];
    
        for (int index = 0; index < alphabetSize; index++ ){
            alphabet[index] = (char) (index + firstLetter);
        }
    
        if (flagToUpperCase){
            alphabet = new String(alphabet).toUpperCase().toCharArray();
        }
    
        return alphabet;
    }
    
    private enum LocaleLanguage{
        ARMENIAN(new Locale("hy"), 'ա', 'ֆ'),
        RUSSIAN(new Locale("ru"), 'а','я'),
        ENGLISH(new Locale("en"), 'a','z');
    
        private final Locale mLocale;
        private final char mFirstLetter;
        private final char mLastLetter;
    
        LocaleLanguage(Locale locale, char firstLetter, char lastLetter) {
            this.mLocale = locale;
            this.mFirstLetter = firstLetter;
            this.mLastLetter = lastLetter;
        }
    
        public Locale getLocale() {
            return mLocale;
        }
    
        public char getFirstLetter() {
            return mFirstLetter;
        }
    
        public char getLastLetter() {
            return mLastLetter;
        }
    
        public String getDisplayLanguage(){
            return getLocale().getDisplayLanguage();
        }
    
        public String getDisplayLanguage(LocaleLanguage locale){
            return getLocale().getDisplayLanguage(locale.getLocale());
        }
    
        @Nullable
        public static LocaleLanguage getLocalLanguage(Locale locale){
            if (locale == null)
                return LocaleLanguage.ENGLISH;
    
            for (LocaleLanguage localeLanguage : LocaleLanguage.values()){
                if (localeLanguage.getLocale().getLanguage().equals(locale.getLanguage()))
                    return localeLanguage;
            }
    
            return null;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 11:31
    char[] abc = new char[26];
    
    for(int i = 0; i<26;i++) {
        abc[i] = (char)('a'+i);
    }
    
    0 讨论(0)
  • 2020-12-02 11:32

    Finally you are getting a char array with alphabet. Why did you do so hard way using a loop ?

    It is just

    char[] alphabet=new char[]{'a','b',.........,'z'}
    
    0 讨论(0)
  • 2020-12-02 11:35

    I think that this ends up a little cleaner, you don't have to deal with the subtraction and indexing:

    char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    
    0 讨论(0)
  • 2020-12-02 11:35

    Simplicity is a virtue. Use this naturally readable array:

    char alphabet[] = {'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'};
    
    0 讨论(0)
  • 2020-12-02 11:38

    Using Java 8 streams

      char [] alphabets = Stream.iterate('a' , x -> (char)(x + 1))
                .limit(26)
                .map(c -> c.toString())
                .reduce("", (u , v) -> u + v).toCharArray();
    
    0 讨论(0)
提交回复
热议问题