我有以下功能:文章生成功能
public ArrayList<ArrayList<Word>> createSentences(ArrayList<ArrayList<Word>> gestures, int startIndex) {
if (gestures.size() == 1) {
return gestures;
}
ArrayList<Word> ret;
ArrayList<ArrayList<Word>> result = new ArrayList<ArrayList<Word>>();
ArrayList<Word> tmp1 = gestures.get(0);
gestures.remove(0);
ArrayList<ArrayList<Word>> tmp2 = createSentences(gestures, startIndex + 1);
for (Word s : tmp1) {
for (Word s2 : tmp2.get(0)) {
ret = new ArrayList<Word>();
ret.add(s);
ret.add(s2);
result.add(ret);
}
}
return result;
}
給定輸入:
ArrayList<ArrayList<Word>> test= new ArrayList<ArrayList<Word>>();
ArrayList<Word> gest1 = new ArrayList<Word>();
gest1.add(new Word("A", "N"));
gest1.add(new Word("B", "V"));
ArrayList<Word> gest2 = new ArrayList<Word>();
gest2.add(new Word("C", "N"));
gest2.add(new Word("D", "V"));
test.add(gest1);
test.add(gest2);
它產生以下結果:
[A, C]
[A, D]
[B, C]
[B, D]
這是很好的,但給出的輸入:
ArrayList<ArrayList<Word>> test= new ArrayList<ArrayList<Word>>();
ArrayList<Word> gest1 = new ArrayList<Word>();
gest1.add(new Word("A", "N"));
gest1.add(new Word("B", "V"));
ArrayList<Word> gest2 = new ArrayList<Word>();
gest2.add(new Word("C", "N"));
gest2.add(new Word("D", "V"));
ArrayList<Word> gest3 = new ArrayList<Word>();
gest3.add(new Word("E", "N"));
gest3.add(new Word("F", "V"));
test.add(gest1);
test.add(gest2);
test.add(gest3);
它產生:
[A, C]
[A, E]
[B, C]
[B, E]
,我想實現這樣的匹配的所有可能的組合:
[A, C, E]
[A, C, F]
[A, D, E]
[A, D, F]
[B, C, E]
[B, C, F]
[B, D, E]
[B, D, F]
有人可以幫我和重寫功能,產生想要的結果。
你有沒有嘗試過,並嘗試調試代碼,看看爲什麼會發生這種情況? – luketorjussen
是的。我已經花了大約4個小時,但所有的混淆,不知道如何解決它。 – Radek