如何在 Java 中对字符串的 ArrayList 进行排序? [英] How can I sort an ArrayList of Strings in Java?
本文介绍了如何在 Java 中对字符串的 ArrayList 进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将 String
随机放入 ArrayList
中.
I have String
s that are put into an ArrayList
randomly.
private ArrayList<String> teamsName = new ArrayList<String>();
String[] helper;
例如:
teamsName.add(helper[0]) where helper[0] = "dragon";
teamsName.add(helper[1]) where helper[1] = "zebra";
teamsName.add(helper[2]) where helper[2] = "tigers" // and so forth up to about 150 strings.
鉴于您无法控制输入(即进入 ArrayList 的字符串是随机的;斑马或龙以任何顺序排列),一旦 ArrayList 填充了输入,我如何按字母顺序对它们进行排序(第一个除外)?
Given the fact that you cannot control the inputs (i.e. string that is coming into the ArrayList is random; zebra or dragon in any order), once the ArrayListis filled with inputs, how do I sort them alphabetically excluding the first one?
teamsName[0]
没问题;按字母顺序对 teamsName[1 to teamName.size]
进行排序.
teamsName[0]
is fine; sort teamsName[1 to teamsName.size]
alphabetically.
推荐答案
Collections.sort(teamsName.subList(1, teamsName.size()));
上面的代码将反映排序后的原始列表的实际子列表.
The code above will reflect the actual sublist of your original list sorted.
这篇关于如何在 Java 中对字符串的 ArrayList 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文