我應該用字母表中的所有字母來填充這個字符串數組,例如:「a」,「b」,「c」,「d」等。如何使用Java中的循環來填充字母數組?
我一直在盯着這個一個小時,我困惑從哪裏開始?今天我剛在課堂上介紹了數組,所以請原諒我的無知。通常不是像這樣填充的數組String [] randomA = {'a','b','c'}等等。所以我很困惑如何用循環填充它。有人可以告訴我爲什麼他們在for循環中以for (char ch =)
開頭,並指出我如何開始的正確方向?
import java.util.Arrays;
public class Fill
{
public static void main(String[] args)
{
String[] letters = new String[26];
for (char ch = . . .; . . .; . . .)
{
letters[. . .] = . . .;
}
System.out.println(Arrays.toString(letters));
}
}
提示:Java中的char類型是一個整數類型。如果以「a」開頭並加1,則會得到下一個字母(「b」)。 –
檢查此問題:https://stackoverflow.com/questions/11671908/can-the-char-type-be-categorized-as-an-integer 您可以遞增字符類型。每個字母對應於ASCII表中唯一代表該字母的唯一值。檢查它也是:https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html –
模板中的「...」指示從何處開始。閱讀[for'循環的語法](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html)。閱讀[如何訪問數組元素](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)。 –