简体   繁体   English

对 Java ArrayList 的一部分进行排序

[英]Sorting a part of Java ArrayList

What is the most efficient way of sorting only a part of ArrayList?仅对 ArrayList 的一部分进行排序的最有效方法是什么? Say all elements from index 0 to 3 in an Arraylist which contains 10 elements.假设包含 10 个元素的 Arraylist 中从索引 0 到 3 的所有元素。

Is there a library function available in Java? Java 中是否有可用的库函数?

Apart from Collections.sort(list) which sorts the entire List!除了对整个 List 进行排序的Collections.sort(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

It is described in the documentation :它在文档中描述:

public List subList(int fromIndex, int toIndex)公共列表 subList(int fromIndex, int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.返回此列表中指定的 fromIndex(包括)和 toIndex(不包括在内)之间的部分的视图。

use the subList [inherited from AbstractList ] method in ArrayList .ArrayList使用subList [从AbstractList继承] 方法。 And then use Collections.sort() on that sub-list.然后在该子列表上使用Collections.sort() That is if writing a highly optimised custom sort function is truly hard work .也就是说,如果编写高度优化的自定义排序函数确实是一项艰巨的工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM