猿问

在这种情况下如何制作子列表列表......?

假设我有一个这样的列表:

[a, b, c, d, e, f, PASS, g, h]

但我希望只要出现这个词,"PASS"就用剩余的数据制作另一个列表。

像这样:

[[a, b, c, d, e, f], [g, h]]


PIPIONE
浏览 143回答 3
3回答

森栏

这是一种使用循环Comparable在分隔符上“拆分” s列表的方法:private static <T extends Comparable<T>> List<List<T>> split(&nbsp; &nbsp; &nbsp; &nbsp;List<T> original, T delimiter) {&nbsp; &nbsp; List<List<T>> res = new ArrayList<>();&nbsp; &nbsp; List<T> current = new ArrayList<>();&nbsp; &nbsp; for (T f : original) {&nbsp; &nbsp; &nbsp; &nbsp; if (f.compareTo(delimiter) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.add(current);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; current.add(f);&nbsp; &nbsp; }&nbsp; &nbsp; if (!current.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; res.add(current);&nbsp; &nbsp; }&nbsp; &nbsp; return res;}它[[a, b, c, d, e, f], [g, h]]在测试时返回:public static void main(String[] args) throws Exception {&nbsp; &nbsp; List<String> list =&nbsp;&nbsp; &nbsp; &nbsp;Arrays.asList("a", "b", "c", "d", "e", "f", "PASS", "g", "h");&nbsp; &nbsp; System.out.println(split(list, "PASS"));}

翻阅古今

这可以写为通用方法(我已经在我的 stackoverflow 答案中使用了它,所以我想我以前在某处读过这个......我会尝试找到具体的位置):private static <T> List<List<T>> splitBy(List<T> list, T delimiter) {&nbsp; &nbsp; int[] indexes = IntStream.rangeClosed(-1, list.size())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(i -> i == -1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || i == list.size()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || delimiter.equals(list.get(i))).toArray();&nbsp; &nbsp; return IntStream.range(0, indexes.length - 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(x -> list.subList(indexes[x] + 1, indexes[x + 1]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // or since java-11, a bit nicer:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // .filter(Predicate.not(List::isEmpty))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(l -> !l.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());}

阿波罗的战车

我安排了简单的解决方案,检查这是否适合您。谢谢你。/**&nbsp;* @author Duminda Hettiarachchi*/import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class StringSubList {ArrayList<String> subList;public StringSubList() {&nbsp; &nbsp; subList = new ArrayList<String>();}public void subList(List<String> list) {&nbsp; &nbsp; int passIndex = list.indexOf("PASS");&nbsp; &nbsp; List<String> list1 = (List<String>) list.subList(0, passIndex);&nbsp; &nbsp; List<String> list2 = (List<String>) list.subList(passIndex+1, list.size());&nbsp; &nbsp; List<List<String>> subLists = new ArrayList<List<String>>();&nbsp; &nbsp; subLists.add(list1);&nbsp; &nbsp; subLists.add(list2);&nbsp; &nbsp; System.out.println("List 1 :" + subLists.get(0));&nbsp; &nbsp; System.out.println("List 2 : " + subLists.get(1));}public static void main(String[] args) {&nbsp; &nbsp; String mainArr[] = {"a", "b", "c", "d", "e", "f", "PASS", "g", "h"};&nbsp; &nbsp; List<String> myList = Arrays.asList("a", "b", "c", "d", "e", "f", "PASS", "g", "h");&nbsp; &nbsp; new StringSubList().subList(myList);&nbsp; &nbsp; &nbsp; &nbsp;`enter code here`&nbsp; &nbsp; }}输出 :列表 1 :[a, b, c, d, e, f]列表 2 : [g, h]
随时随地看视频慕课网APP

相关分类

Java
我要回答