在這個問題中,我將編寫一個方法,它接受strings
的ArrayList
和integer
。在這種情況下,該整數將按照整數所指示的次數重複單個單詞。例如,如果列表在調用方法前存儲值["how", "are", "you?"]
,並且k爲4,則應存儲值["how", "how", "how", "how", "are", "are", "are", "are", "you?", "you?", "you?", "you?"]
如果k爲0或否定,則在調用後該列表應該爲空。在一個數組中重複序列
public static void stutter(ArrayList<String> thing, int k) {
if (k <= 0) {
thing.clear(); // if k is 0
return;
}
for (int i = 0; i< thing.size(); i+= k) {
String temp = thing.get(i);
for (int j = 0; j < k; j++) {
thing.add(temp);
}
}
}
那麼你面臨的問題是什麼? – dharam
我把它運用起來,沒關係。 –