仅对 ArrayList 的一部分进行排序的最有效方法是什么?假设包含 10 个元素的 Arraylist 中从索引 0 到 3 的所有元素.
What is the most efficient way of sorting only a part of ArrayList? Say all elements from index 0 to 3 in an Arraylist which contains 10 elements.
Java 中是否有可用的库函数?
Is there a library function available in Java?
除了对整个 List 进行排序的 Collections.sort(list)!
Apart from Collections.sort(list) which sorts the entire List!
编写高度优化的自定义排序函数需要一些工作.
Writing a highly optimised custom sort function will take some work.
推荐答案 Collections.sort(list.subList(0,3)); Note: '3' here is excluded from sorting文档:
public List subList(int fromIndex,int toIndex)
public List subList(int fromIndex, int toIndex)
返回此列表中指定部分之间的视图fromIndex(包含)和 toIndex(不包含).
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
更多推荐
对 Java ArrayList 的一部分进行排序
发布评论