对 Java ArrayList 的一部分进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5164902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:46:04  来源:igfitidea点击:

Sorting a part of Java ArrayList

javasortingarraylist

提问by Nitish Upreti

What is the most efficientway of sorting only a part of ArrayList? Say all elements from index 0 to 3 in an Arraylist which contains 10 elements.

仅对 ArrayList 的一部分进行排序的最有效方法是什么?假设包含 10 个元素的 Arraylist 中从索引 0 到 3 的所有元素。

Is there a library function available in Java?

Java 中是否有可用的库函数?

Apart from Collections.sort(list)which sorts the entire List!

除了Collections.sort(list)排序整个列表!

Writing a highly optimised custom sort function will take some work.

编写一个高度优化的自定义排序函数需要一些工作。

回答by Suraj Chandran

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)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

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

返回此列表中指定的 fromIndex(包括)和 toIndex(不包括在内)之间的部分的视图。

回答by anirvan

use the subList[inherited from AbstractList] method in ArrayList. And then use Collections.sort()on that sub-list. That is if writing a highly optimised custom sort function is truly hard work.

中使用subList[继承自AbstractList] 方法ArrayList。然后Collections.sort()在该子列表上使用。也就是说,如果编写高度优化的自定义排序函数确实是一项艰巨的工作