更好的方法来生成字母表中所有字母的数组
现在我正在做
for (char c = 'a'; c <= 'z'; c++) { alphabet[c - 'a'] = c; }
但有没有更好的方法来做到这一点? 类似于Scala的'a' to 'z'
我认为这样会更清洁一点,你不必处理减法和索引:
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
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'};
这是一个有趣的unicode解决scheme。
char[] alpha = new char[26] int k = 0; for(int i = 0; i < 26; i++){ alpha[i] = (char)(97 + (k++)) }
这会生成一个较低版本的字母表,如果你想要大写,你可以用'65'replace'97'。
希望这可以帮助。
检查一次,我确定你会得到a
z
字母:
for (char c = 'a'; c <= 'z'; c++) { al.add(c); } System.out.println(al);'
如果您正在使用Java 8
char[] charArray = IntStream.rangeClosed('A', 'Z') .mapToObj(c -> "" + (char) c).collect(Collectors.joining()).toCharArray();
static String[] AD = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "G", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "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"};//Alphabet&Digit
最后你得到一个字母array
。 你为什么这么用loop
呢?
这是公正的
char[] alphabet=new char[]{'a','b',.........,'z'}
定义任何语言的枚举,并调用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);
结果
I / System.out:安装在我的系统上
我/ 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; } }
char[] abc = new char[26]; for(int i = 0; i<26;i++) { abc[i] = (char)('a'+i); }
import java.util.*; public class Experiments{ List uptoChar(int i){ char c='a'; List list = new LinkedList(); for(;;) { list.add(c); if(list.size()==i){ break; } c++; } return list; } public static void main (String [] args) { Experiments experiments = new Experiments(); System.out.println(experiments.uptoChar(26)); }
for (char letter = 'a'; letter <= 'z'; letter++) { System.out.println(letter); }