Java: split a List into two sub-Lists?

后端 未结 14 1537
执笔经年
执笔经年 2020-12-08 02:29

What\'s the simplest, most standard, and/or most efficient way to split a List into two sub-Lists in Java? It\'s OK to mutate the original List, so no copying should be nece

14条回答
  •  天命终不由人
    2020-12-08 03:04

    Riffing on Marc's solution, this solution uses a for loop that saves some calls to list.size():

     List split(List list, int i) {
        List x = new ArrayList(list.subList(i, list.size()));
        // Remove items from end of original list
        for (int j=list.size()-1; j>i; --j)
            list.remove(j);
        return x;
    }
    

提交回复
热议问题