我想要編寫的代碼將字符串拆分爲不同的子字符串,每個字符串包含3個字符 並將它們分配給字符串數組,例如字符串「abcdefg」可以是「abc」,「bcd 「,」cde「,」efg「,並將它們分配給一個字符串數組。我有以下的代碼這是越來越錯誤:數組越界錯誤
String[] words = new String[] {};
String sequence = "abcdefg";
int i;
for(i = 0; i <= sequence.length()-3; i++) {
words[i] = sequence.substring(i, 3+i);
System.out.println(words[i]);
}
它的工作原理,謝謝很多 – 2014-11-03 08:31:29