如何对 ArrayList<String> 进行排序基于特定的索引范围 [英] How to sort an ArrayList&lt;String&gt; based on specific index range

查看:30
本文介绍了如何对 ArrayList<String> 进行排序基于特定的索引范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据特定的索引范围对字符串的 ArrayList 进行排序.例如,我在列表中有以下项目: ["abc", "xyz", "pqr" , "asd"]现在我想将此列表从索引 1 排序到最后一个索引.

My need is to sort the ArrayList of Strings, based on specific index range. For example I have following items in list: ["abc", "xyz", "pqr" , "asd"]Now I want to sort this list from index 1 till last index.

一种方法 因为我认为我可以从具有所需索引范围的主列表创建一个子列表,对其进行排序并相应地添加子列表.但我的问题是:

One way As I think that I can create a sub-list from main list with desired index range, sort it and add the sub-list accordingly. But my question is:

是否有任何可用的 API?或者任何其他更快的方法来实现这一点.

Is there any API already available for that? Or any other faster way to achieve this.

推荐答案

你应该做的

Collections.sort(yourList.subList(1, yourList.size()));

由于 List.subList 方法返回列表的 viewCollections.sort 所做的修改也会影响后备列表.

Since the List.subList method returns a view of the list, modifications done by Collections.sort will affect the backing list as well.

这篇关于如何对 ArrayList<String> 进行排序基于特定的索引范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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