更好的方法来生成字母表中所有字母的数组

时间:2013-07-10 16:18:16

标签: java

现在我正在做

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

但是有更好的方法吗?与Scala的'a' to 'z'

类似

18 个答案:

答案 0 :(得分:162)

我认为这样会更清洁,你不必处理减法和索引:

char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();

答案 1 :(得分:26)

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'};

答案 2 :(得分:12)

这是一个有趣的unicode解决方案。

char[] alpha = new char[26]
for(int i = 0; i < 26; i++){
    alpha[i] = (char)(97 + i)
}

这会生成一个较低版本的字母,如果你想要大写,你可以用'65'替换'97'。

希望这有帮助。

答案 3 :(得分:9)

将任何语言定义为枚举,并调用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;
    }
}

答案 4 :(得分:6)

如果您使用的是Java 8

char[] charArray = IntStream.rangeClosed('A', 'Z')
    .mapToObj(c -> "" + (char) c).collect(Collectors.joining()).toCharArray();

答案 5 :(得分:6)

在具有Stream API的Java 8中,您可以执行此操作。

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

答案 6 :(得分:4)

检查一次,我确定您会收到az个字母:

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

答案 7 :(得分:2)

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

答案 8 :(得分:2)

io.vavr

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="main">
  <option value="1" selected>One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
  <option value="4">Four</option>
  <option value="5">Five</option>
</select>
<select >
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
  <option value="4">Four</option>
  <option value="5">Five</option>
</select>
<select >
  <option value="1">One</option>
  <option value="2" selected >Two</option>
  <option value="3">Three</option>
  <option value="4">Four</option>
  <option value="5">Five</option>
</select>

答案 9 :(得分:2)

以下是根据汤姆·托马斯的回答的几种选择。

字符数组:

        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(","));

答案 10 :(得分:1)

简单是一种美德。使用此自然可读的数组:

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'};

答案 11 :(得分:0)

最后,您将获得一个带字母的char array。你为什么用loop

这么做

只是

char[] alphabet=new char[]{'a','b',.........,'z'}

答案 12 :(得分:0)

char[] abc = new char[26];

for(int i = 0; i<26;i++) {
    abc[i] = (char)('a'+i);
}

答案 13 :(得分:0)

使用Java 8流

  char [] alphabets = Stream.iterate('a' , x -> (char)(x + 1))
            .limit(26)
            .map(c -> c.toString())
            .reduce("", (u , v) -> u + v).toCharArray();

答案 14 :(得分:0)

要获取除小写字母之外的大写字母,您还可以执行以下操作:

model.compile(loss='binary_crossentropy',
              optimizer=Adam(lr=.0001),
              metrics=['accuracy']) 

答案 15 :(得分:0)

在 kotlin 中你可以这样做

var alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray()

答案 16 :(得分:-1)

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

答案 17 :(得分:-6)

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