2017-10-21 6 views
-2

文字配列をchar配列に変換する方法はありますか? toCharArrayちょうどStringクラスエラーが発生しました:比類のない型:charとString

String alphabet = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"; 
char[] charArray = alphabet.toCharArray(); 

の方法ですので、あなたがStringオブジェクトを使用する必要が

error: cannot find symbol 
     char[] charArray = alphabet.toCharArray(); 
+2

をcharに変換されますか? –

+1

文字列の配列ではなく文字列が必要です。 –

答えて

1

このコードは、すべての `STRING`配列を使用する必要がないのはなぜ文字列配列は、配列

String[] alphabet = {"a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z"}; 
String s = ""; 
for (String n:alphabet) 
    s+= n; 
char[] c = s.toCharArray(); 
+2

なぜこのようなことをするのですか? –

+0

@Aominè実際には、問題は文字列の配列からcharの配列を取得することでした。この型キャスティングは避けることができますが、これが問題でした –

3

String[] alphabet = {"a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z"}; 
    char[] charArray = alphabet.toCharArray(); 

現在、これは私のエラーメッセージを表示します。現在、私は、コードを持っていますあなたがまだ文字列配列が必要な場合は空の文字列に分割するだけです

String [] arr = alphabet.split(""); // your String array 
関連する問題