当前位置:   article > 正文

android中数组自动排序,关于Java:在android中对字符串arraylist进行排序

android 字符串数组有序排列

本问题已经有最佳答案,请猛点这里访问。

我有一个名为名称的字符串arraylist。 如何按字母顺序对数组列表排序?

杜德(Dude),谷歌" sort arraylist java"。 第一次点击会显示答案。 这个问题与Android(纯Java)无关。

@Flo确实是,但是后来我尝试在此处链接一个dupe,并且一切都变得更加复杂,对于基本的东西我找不到任何问题(关闭是对ArrayList进行排序)。 可能在这里,但我可能错过了。

ArrayList names = new ArrayList();

names =fillNames() // whatever method you need to fill here;

Collections.sort(names);

http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29

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, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

String实现Comparable:

java.lang Class String

java.lang.Object extended by

java.lang.String

All Implemented Interfaces:

Serializable, CharSequence, Comparable

http://download.oracle.com/javase/6/docs/api/java/lang/String.html

另一种解决方案,但Collections.sort()是最好的。 我只想显示替代

ArrayList strings = new ArrayList();

strings.add("a");

strings.add("ab");

strings.add("aa");

String[] stringsArray = new String[strings.size()];

strings.toArray(stringsArray);

Arrays.sort(stringsArray);

List sorted = Arrays.asList(stringsArray);

System.out.println(sorted);

导入java.util.Collections;

然后使用Collections.sort();

我正在使用android platform。是否可以在android中使用相同的代码?

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/715592
推荐阅读
相关标签
  

闽ICP备14008679号