要求如图,以下是我的代码,请大神们指教一下哪里需要改正:
import java.util.ArrayList;
import java.util.List;
public class StingList01 {
public static void main(String[] args) {
String s = "abcddfcdababcd";
String start = "ab";
String end = "cd";
//调用方法allaubsting传入参数 接收集合输出结果
List<String> allstringlist = allaubstring(s, start, end);
System.out.println(allstringlist);
}
public static List<String> allaubstring(String s, String start, String end) {
List<String> stringList = new ArrayList<>();
List<Integer> startList = new ArrayList<>();
List<Integer> endList = new ArrayList<>();
//start出现的索引位置
for (int si = s.indexOf(start) - 1; si < s.lastIndexOf(start); ) {
si = s.indexOf(start, si + 1);
startList.add(si);
}
System.out.println(startList);//start在s中的位置索引集合
//end出现的索引位置
for (int ei = s.indexOf(end) - 1; ei < s.lastIndexOf(end); ) {
ei = s.indexOf(end, ei + 1);
endList.add(ei);
}
System.out.println(endList);//end在s中的位置索引集合
//循环嵌套 当end索引大于start索引时 可以组成字符串 加入集合
for (int i = 0; i < startList.size(); i++) {
for (int i1 = 0; i1 < endList.size(); i1++) {
if (endList.get(i1) > startList.get(i)) {
String substring = s.substring(startList.get(i), endList.get(i1) + end.length());
stringList.add(substring);
}
}
}
return stringList;
}
}