String [] str = {"a","b","c","d"}; 求这四个元素能组成的不重复list a,b和 b,a 算同一个,求源码
import java.util.HashSet; import java.util.Set; public class ABCTest { public static void main(String[] args) { String[] str = {"a","b","c","d"}; Set<String> list = new HashSet<>(); for (int i = 0; i < str.length; i++) { list.add(str[i]); for (int j = i+1; j < str.length; j++) { list.add(str[i]+str[j]); for (int k = j+1; k < str.length; k++) { list.add(str[i]+str[j]+str[k]); for (int l = k+1; l < str.length; l++) { list.add(str[i]+str[j]+str[k]+str[l]); } } } } System.out.println(list); } }
输出结果:[a, ab, bc, cd, abd, ac, b, bcd, bd, abc, acd, ad, c, d, abcd]
帮忙采纳我的回答!谢谢!!!
/***
* 源自 https://www.cnblogs.com/zlingh/p/4040742.html
*/
public class SetTest {
public static void main(String[] args) {
String str[] = { "A", "B", "C", "D", "E" };
int nCnt = str.length;
int nBit = (0xFFFFFFFF >>> (32 - nCnt));
for (int i = 1; i <= nBit; i++) {
for (int j = 0; j < nCnt; j++) {
if ((i << (31 - j)) >> 31 == -1) {
System.out.print(str[j]);
}
}
System.out.println("");
}
}
}