问题描述
限时送ChatGPT账号..我有一个名为 names 的字符串数组列表.如何按字母顺序对数组列表进行排序?
I have a string arraylist called names. How do I sort the arraylist in alphabetical order?
推荐答案
ArrayList<String> names = new ArrayList<String>();
names =fillNames() // whatever method you need to fill here;
Collections.sort(names);
http://download.oracle/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29
根据其元素的自然顺序,将指定列表按升序排序.列表中的所有元素都必须实现 Comparable 接口.此外,列表中的所有元素必须相互比较(即,e1pareTo(e2) 不得为列表中的任何元素 e1 和 e2 抛出 ClassCastException).
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1pareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
String
实现 Comparable
:
java.lang 类字符串
java.lang Class String
java.lang.Object 扩展为java.lang.String
java.lang.Object extended by java.lang.String
所有实现的接口:可序列化、CharSequence、可比较
All Implemented Interfaces: Serializable, CharSequence, Comparable
http://download.oracle/javase/6/docs/api/java/lang/String.html
这篇关于在android中排序字符串arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论