对 Java ArrayList 的一部分进行排序

编程入门 行业动态 更新时间:2024-10-22 12:21:19
本文介绍了对 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 的一部分进行排序

本文发布于:2023-06-07 22:01:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568552.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   ArrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!