对 Java ArrayList 的一部分进行排序 [英] Sorting a part of Java ArrayList

查看:29
本文介绍了对 Java ArrayList 的一部分进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅对 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 的一部分进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆