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 to Scala's 'a' to 'z'
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 to Scala's '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'};
此getAlphabet
方法使用与该问题类似的技术来为任意语言生成字母表。
将任何语言定义为枚举,然后调用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:աբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆ
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;
}
}
这是一个有趣的 Unicode 解决方案:
char[] alpha = new char[26]
for(int i = 0; i < 26; i++){
alpha[i] = (char)(97 + i)
}
这会生成一个小写版本的字母,如果你想要大写,你可以用 '65' 替换 '97'。
在带有 Stream API 的 Java 8 中,您可以执行此操作。
IntStream.rangeClosed('A', 'Z').mapToObj(var -> (char) var).forEach(System.out::println);
如果您使用的是 Java 8
char[] charArray = IntStream.rangeClosed('A', 'Z')
.mapToObj(c -> "" + (char) c).collect(Collectors.joining()).toCharArray();
static String[] AlphabetWithDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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", "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"};
一旦我确定你会得到字母,请检查a
这个z
:
for (char c = 'a'; c <= 'z'; c++) {
al.add(c);
}
System.out.println(al);'
以下是基于@tom thomas的回答的一些替代方案。
char[] list = IntStream.concat(
IntStream.rangeClosed('0', '9'),
IntStream.rangeClosed('A', 'Z')
).mapToObj(c -> (char) c+"").collect(Collectors.joining()).toCharArray();
注意:如果您的分隔符也是值之一,则无法正常工作。
String[] list = IntStream.concat(
IntStream.rangeClosed('0', '9'),
IntStream.rangeClosed('A', 'Z')
).mapToObj(c -> (char) c+",").collect(Collectors.joining()).split(",");
注意:如果您的分隔符也是值之一,则无法正常工作。
List<String> list = Arrays.asList(IntStream.concat(
IntStream.rangeClosed('0', '9'),
IntStream.rangeClosed('A', 'Z')
).mapToObj(c -> (char) c+",").collect(Collectors.joining()).split(","));
使用io.vavr
public static char[] alphanumericAlphabet() {
return CharSeq
.rangeClosed('0','9')
.appendAll(CharSeq.rangeClosed('a','z'))
.appendAll(CharSeq.rangeClosed('A','Z'))
.toCharArray();
}
对于搜索 Kotlin 解决方案并在此处结束的 Android 开发人员:
// Creates List<Char>
val chars1 = ('a'..'z').toList()
// Creates Array<Char> (boxed)
val chars2 = ('a'..'z').toList().toTypedArray()
// Creates CharArray (unboxed)
val chars3 = CharArray(26) { 'a' + it }
// Creates CharArray (unboxed)
val chars4 = ('a'..'z').toArray()
fun CharRange.toArray() = CharArray(count()) { 'a' + it }
要了解我所说的“装箱”和“未装箱”的意思,请参阅这篇文章。
非常感谢这个 Kotlin 讨论线程。
简单是一种美德。使用这个自然可读的数组:
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'};
使用 Java 8 流
char [] alphabets = Stream.iterate('a' , x -> (char)(x + 1))
.limit(26)
.map(c -> c.toString())
.reduce("", (u , v) -> u + v).toCharArray();
char[] abc = new char[26];
for(int i = 0; i<26;i++) {
abc[i] = (char)('a'+i);
}
要获取除小写字母之外的大写字母,您还可以执行以下操作:
String alphabetWithUpper = "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz".toUpperCase();
char[] letters = alphabetWithUpper.toCharArray();
const alphabetArray = "abcdefghijklmnopqrstuvwxyz".split("");
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));
}
在科特林你可以做到这一点
var alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray()
for (char letter = 'a'; letter <= 'z'; letter++)
{
System.out.println(letter);
}