public static Boolean check(List<String> list, String city) {
for (String s : list) {
if (s.equals(city)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
List<String> city = new ArrayList<String>();//1,3,7,13
city.add("上海");
city.add("北京");
city.add("深圳");
city.add("南京");
List<String> tempList = new ArrayList<String>();
for (String c : city) {
StringBuffer str = new StringBuffer();
str.append(c);
if (!tempList.contains(c)) {
tempList.add(c);
}
StringBuffer str2 = new StringBuffer();
for (String s : city) {
if (str2.length() > 0) {
str2.append(",").append(s);
} else {
str2.append(s);
}
String[] split2 = str2.toString().split(",");
Arrays.sort(split2);
String data2 = String.join(",", split2);
if (!tempList.contains(data2) && !check(tempList, data2)) {
tempList.add(data2);
} else {
str2 = new StringBuffer();
}
if (!c.equals(s)) {
if (str.length() > 0) {
str.append(",").append(s);
} else {
str.append(s);
}
String[] split = str.toString().split(",");
Arrays.sort(split);
String data = String.join(",", split);
if (!tempList.contains(data) && !check(tempList, data)) {
tempList.add(data);
}
}
}
String[] split = str.toString().split(",");
Arrays.sort(split);
String data = String.join(",", split);
if (!tempList.contains(data) && !check(tempList, data)) {
tempList.add(data);
}
}
tempList.forEach(e -> {
System.out.println(e);
});
}
上海
北京
上海,北京
北京,深圳
上海,北京,深圳
北京,南京,深圳
上海,北京,南京,深圳
深圳
南京,深圳
上海,深圳
南京
上海,南京
上海,北京,南京