Java 字符串列表的组合
下面是我需要在特定条件下组合的字符串列表 “MSD”、“EEE”、“RSR”、“OCL”、“SMS”、“RTS” 组合的条件如下:Java 字符串列表的组合,java,math,puzzle,Java,Math,Puzzle,下面是我需要在特定条件下组合的字符串列表 “MSD”、“EEE”、“RSR”、“OCL”、“SMS”、“RTS” 组合的条件如下: 组合应至少有两个字符串 (例如:(“EEE” ,“RSR”),(“EEE”、“RSR”、“OCL”)) 组合应该由相邻字符串组成(例如:(“OCL”、“SMS”)、(“MSD”、“EEE”、“RSR”)是有效的。但不是(“EEE”、“OCL”)。因为“EEE”和“OCL” (彼此不相邻) 对于这个问题,Java实现非常受欢迎 public class Dummy {
,“RSR”),(“EEE”、“RSR”、“OCL”))
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);
}
}
}
公共类虚拟{
公共静态void main(字符串[]args){
字符串[]str={“MSD”、“EEE”、“RSR”、“OCL”、“SMS”、“RTS”};
列表=新的ArrayList();
对于(int j=0;j
抱歉,我的试用代码更新太晚了,您尝试了什么?换行符有效吗?像rts msd?是的,只要它们相邻。“eee rsr”有效,但“rsr eee”无效,因此“rts msd”也无效
for (int j = 0; j < str.length; j++) {
String temp = "";
for (int i = j; i < str.length; i++) {
if ("".equals(temp))
temp = str[i]; // assign the String to temp, but do not add to list yet
else {
temp = temp + " " + str[i];
list.add(temp); // now that temp consists of at least two elements
// add them to the list
}
}
}
MSD EEE
MSD EEE RSR
MSD EEE RSR OCL
MSD EEE RSR OCL SMS
MSD EEE RSR OCL SMS RTS
EEE RSR
EEE RSR OCL
EEE RSR OCL SMS
EEE RSR OCL SMS RTS
RSR OCL
RSR OCL SMS
RSR OCL SMS RTS
OCL SMS
OCL SMS RTS
SMS RTS