1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
我認(rèn)為實(shí)現(xiàn)這一目標(biāo)的最簡單方法是遞歸。
假設(shè)您希望每個(gè)范圍具有相同的限制(即 i、j、k 等從 0..100 開始),您可以這樣做:
void recursive(List<Integer> values, int depth) {
if (values.size() == depth) {
// Do the thing you want to do with the values, i.e. the "innermost loop".
} else {
// This is intentionally Integer, so that remove removes that value, not the element at that index.
for (Integer a = 0; a <= 100; ++a) {
values.add(a);
recursive(values, depth);
values.remove(a);
}
}
}
當(dāng)depth列表中的值少于值時(shí),這會(huì)依次將范圍內(nèi)的每個(gè)值添加到列表中,然后遞歸。
一旦列表中有足夠的值,它就會(huì)做你想做的“事情”。該列表將包含depth值,您可以使用 訪問單個(gè)值values.get(i)。
添加回答
舉報(bào)