在Java中将List分成两个子列表的最简单,最标准和/或最有效的方法是什么?可以改变原始列表,因此不需要复制。方法签名可以是
/** Split a list into two sublists. The original list will be modified to
* have size i and will contain exactly the same elements at indices 0
* through i-1 as it had originally; the returned list will have size
* len-i (where len is the size of the original list before the call)
* and will have the same elements at indices 0 through len-(i+1) as
* the original list had at indices i through len-1.
*/
List split(List list, int i);
[EDIT] List.subList返回原始列表上的视图,如果原始视图被修改,则视图无效。所以split不能使用子列表,除非它也免除了原始引用(或者,如在Marc Novakowski的答案,使用subList,但立即复制结果)。