`
PlayGod1984
  • 浏览: 161872 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

ArrayList排序

阅读更多
今天遇到ArrayList排序,说起来真是汗,居然不知道,基础是够差,网上查阅一番。发现用到接口Comparator(定义排序规则,即ArrayList里面的对象应该按什么规则排序),可以自己用类实现这个接口,实现compare接口,这个接口返回值即为你的规则。升序降序都可以自己来定,然后OK了,只要用集合类Collections的sort方法就可以了。sort有两个参数,第一个参数是你的比较规则类,第二个则是你的List。方便啊。
恶补基础
======================================================================
sort
public static <T> void sort(List<T> list,
                            Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place. 


Parameters:
list - the list to be sorted.
c - the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used. 
Throws: 
ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator. 
UnsupportedOperationException - if the specified list's list-iterator does not support the set operation.
See Also:
Comparator


======================================================================
分享到:
评论

相关推荐

    arrayList排序

    - 对于大量数据的排序,ArrayList的性能可能不如使用LinkedList,因为ArrayList排序时涉及到大量的索引计算和元素移动。 - 如果ArrayList中元素的类型不一致,`Collections.sort()`会抛出ClassCastException异常,...

    ArrayList排序.txt

    通用ArrayList 排序

    arr.rar_C# ArrayList 排序_arraylist_arraylist 排序_数组排序

    本篇文章将深入探讨ArrayList的排序以及数组排序的相关知识点。 首先,ArrayList的排序主要依赖于`Sort()`方法。这个方法是ArrayList类中的一个成员,它通过快速排序算法对ArrayList中的元素进行原地排序。快速排序...

    ArrayList排序和遍历补充案例.java

    ArrayList排序和遍历补充案例.java

    java的arraylist排序示例(arraylist用法)

    在这个示例中,我们将深入探讨如何对ArrayList中的对象进行排序,以及ArrayList的一些基本用法。 首先,我们创建了一个名为`Test`的类,其中包含主方法`main`,这是程序的入口点。在`main`方法中,我们实例化了几个...

    java中对ArrayList进行排序

    java中对ArrayList进行排序的方法参考

    java对ArrayList排序代码示例

    然而,ArrayList本身并不提供排序功能,但我们可以借助Java的Collections类提供的sort()方法来实现排序。本文将通过三个示例详细讲解如何在Java中对ArrayList进行排序。 首先,我们来看一个简单的例子,创建一个名...

    java教程之二个arraylist排序的示例分享

    总结一下,Java中对ArrayList排序的步骤包括: 1. 创建一个ArrayList,并填充需要排序的对象。 2. 实现Comparator接口,创建一个或多个比较器类,覆盖`compare()`方法,定义排序规则。 3. 使用`Collections.sort()`...

    基于ArrayList的对象多属性值排序

    在Java编程中,基于ArrayList的对象多属性值排序是一项常见的任务,尤其在处理数据集合时。ArrayList是Java集合框架的一部分,它提供了动态数组的功能,允许我们存储和操作对象数组。本主题将深入探讨如何对...

    Java ArrayList实现的快排,归并排序,堆排序

    本篇将重点讲解如何利用ArrayList实现快速排序(Quick Sort)、归并排序(Merge Sort)和堆排序(Heap Sort)这三种经典的排序算法,并探讨它们的原理、优缺点以及在实际应用中的选择。 **快速排序(Quick Sort)**...

    ArrayList反转排序1

    本篇文章将深入探讨如何使用ArrayList进行反转排序。在给出的标题和描述中,我们看到了两种方法,分别是反转ArrayList和对ArrayList进行排序。 首先,我们来看如何反转ArrayList。在Java中,我们可以使用`...

    Java系列ArrayList

    ArrayList排序 ArrayList本身不支持排序,但可以通过`Collections.sort()`方法对其进行排序: ```java Collections.sort(list); ``` ### 特别注意 ArrayList是非线程安全的,这意味着在多线程环境下,多个线程...

    arraylist 对象 数组排序

    当我们说“arraylist对象中的某个值排序”时,这通常指的是对ArrayList中的元素,而不是ArrayList本身进行排序。ArrayList不支持直接排序,但是我们可以通过调用Collections.sort()方法或者自定义Comparator来实现。...

    JAVA 键盘录入学生成绩,排序后输出

    7. **集合的遍历与排序输出**:在完成数据录入并排序后,可以通过迭代Set(如`TreeSet`)或使用`Collections.sort()`方法对ArrayList排序,然后逐个输出每个学生的相关信息。 在Eclipse这样的集成开发环境中,可以...

    有关于C#的程序(ArrayList类,动态添加,删除的)

    ArrayList提供了丰富的成员方法来操作其中的元素,如添加、删除、查找和排序等。 2. **动态添加元素** - `Add`: 使用Add方法可以在ArrayList的末尾添加一个元素。例如,`myArrayList.Add(someObject)` 将对象...

    对ArrayList里装载的对象进行自定义排序(任意对象、任何字段、任何规则) !

    对ArrayList里装载的对象进行自定义排序(任意对象、任何字段、任何规则) ! ArrayList中可装载任何对象,并以指定的该对象的字段对该集合中的对象进行指定形式的排序(正序、反序)。 任何类通用........

    ASP .NET - ArrayList对象

    这将按照默认的排序规则(通常是字典顺序)对`ArrayList`中的元素进行排序。 #### 反转元素顺序 如果需要反转排序后的元素顺序,可以使用`Reverse()`方法: ```vb mycountries.Reverse() ``` 这将使`ArrayList`...

    以前的算法小技巧1

    2. **ArrayList排序**:ArrayList是基于动态数组实现的列表,其内部实现支持排序。对整数ArrayList进行排序,可以直接调用`Collections.sort()`方法,因为整数实现了`Comparable`接口。而如果ArrayList包含自定义...

Global site tag (gtag.js) - Google Analytics