java - Listing all the combinations of the elements of a set -
among elements of set, want list 3-combinations of set. there way it?
if list;
for(int i=0; i<list.size()-2; i++) for(int j=i+1; j<list.size()-1; j++) for(int k=j+1; k<list.size(); k++) system.out.println(list.get(i) + " " + list.get(j) + " " + list.get(k));
but how set without converting list?
converting list , using logic code first choice. if want without converting list, can iterators, this:
set<string> set = ...; (string : set) { boolean bgo = false; (string b : set) { if (bgo) { boolean cgo = false; (string c : set) { if (cgo) { system.out.println(a + " " + b + " " + c); } else if (b.equals(c)) { cgo = true; } } } else if (a.equals(b)) { bgo = true; } } }
the logic above freely iterates set in outer loop. when starts iterating set in first nested loop, skips elements until current element of outer loop found (i.e. until a.equals(b)
). after that, runs third nested loop, skips data in set until b
, @ point starts producing combinations output.
Comments
Post a Comment