// adds a new inner list with a single item (string)
static void withNewList(ArrayList<List<String>> lists, String string) {
List<String> newList1 = new ArrayList<>();
newList1.add(string);
lists.add(newList1);
}
static void withNewString(ArrayList<List<String>> lists, String string) {
// if the last inserted list has a same item
List<String> lastList = lists.get(lists.size() - 1);
if (lastList.contains(string)) {
// append it to the last inner list
lastList.add(string);
} else {
// or else create a new list with a single item (string)
withNewList(lists, string);
}
}
2条答案
按热度按时间hyrbngr71#
为了方便导航到最后一个(上一个)项目,我将使用linkedlist的linkedlist:
ajsxfq5m2#
这是非常有可能使用可变的减少导致
List<List<String>>
. 为了更好的可读性,我建议分为以下几个方法:考虑到以下几点
list
输入:... 打印结果时,输出如下所示:
[[id1, id1, id1], [id2, id2], [id3, id3], [id2, id2], [id4]]