字符串列表的java组合
下面是我需要在特定条件下组合的字符串列表
“MSD”、“EEE”、“RSR”、“OCL”、“SMS”、“RTS”
组合的条件如下:
- 组合应该至少有两个字符串
(例如:(“EEE”
(“EEE”、“RSR”、“OCL”)) - 组合应该由相邻字符串组成(例如:(“OCL”、“SMS”)、(“MSD”、“EEE”、“RSR”)有效。但不是(“EEE”、“OCL”)。自从“EEE”和“OCL” (彼此不在一起)
对于这个问题,Java实现非常受欢迎
public class Dummy {
public static void main(String[] args) {
String[] str = { "MSD" ,"EEE", "RSR", "OCL", "SMS","RTS" };
List<String> list = new ArrayList<>();
for (int j = 0; j < str.length; j++) {
String temp = "";
for (int i = j; i < str.length; i++) {
temp = temp + " " + str[i];
list.add(temp);
}
}
for (String string : list) {
System.out.println(string);
}
}
}
抱歉,我的代码更新太晚了
# 1 楼答案
修复了单个条目也被列出的问题。从而导致: