arraylist排序(arraylist排序问题)

更新时间:2023-02-28 21:31:09 阅读: 评论:0

arraylist数组排序

用Collections工具方法

importjava.util.Collections;
importjava.util.Comparator;
//按薪水由低到高排序
publicvoidsort(){
Collections.sort(arr,newComparator(){

publicintcompare(Objecto1,Objecto2){
Empe1=(Emp)o1;
Empe2=(Emp)o2;

returnFloat.compare(e1.sla,e2.sla);
}
});
}

如何实现对ArrayList排序 sort

使用Collections.sort()传入ArrayList,会采用默认的方式进行排序(字典序)
使用Collections.sort()传入ArrayList和自己实现Commparator接口的类的对象,实现自定义排序
使用List.sort()传入自己实现Commparator接口的类的对象,实现自定义排序
Comparator返回值在jdk1.7、jdk1.8里必须是一对相反数,如1和-1,不能是1和0.因为1.7的排序算法采用timsort,对返回值有严格要求

如何实现对ArrayList排序 sort

package com.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
public static void main(String[] args) {

Student zlj = new Student("丁晓宇", 21);
Student dxy = new Student("赵四", 22);
Student cjc = new Student("张三", 11);
Student lgc = new Student("刘武", 19);

List<Student> studentList = new ArrayList<Student>();
studentList.add(zlj);
studentList.add(dxy);
studentList.add(cjc);
studentList.add(lgc);

System.out.println("按年龄升序:");
Collections.sort(studentList, new SortByAge());
for (Student student : studentList) {
System.out.println(student.getName() + " / " + student.getAge());
}
System.out.println();
System.out.println("按姓名排序:");
Collections.sort(studentList, new SortByName());
for (Student student : studentList) {
System.out.println(student.getName() + " / " + student.getAge());
}
}
}

class SortByAge implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return s1.getAge().compareTo(s2.getAge());
// if (s1.getAge() > s2.getAge())
// return 1;
// return -1;
}
}

class SortByName implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return s1.getName().compareTo(s2.getName());
}
}

输出结果:
按年龄升序:
张三 / 11
刘武 / 19
丁晓宇 / 21
赵四 / 22

按姓名排序:
丁晓宇 / 21
刘武 / 19
张三 / 11
赵四 / 22

java如何对Arraylist数组进行排序(用comparable)

看代码:

importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;

publicclassDemo{

publicstaticvoidmain(String[]args)throwsException{
Pair[]pairs={
newPair(0,1),
newPair(2,9),
newPair(7,0),
newPair(8,8),
newPair(8,6),
newPair(9,2),
newPair(1,5),
newPair(8,2),
newPair(9,15),
newPair(9,5)
};
ArrayList<Pair>pairList=newArrayList<>(Arrays.asList(pairs));

System.out.println("排序前:");
System.out.println(Arrays.toString(pairs));
Arrays.sort(pairs);//对数组排序
System.out.println("排序后:");
System.out.println(Arrays.toString(pairs));

System.out.println("排序前:");
System.out.println(pairList);
Collections.sort(pairList);//对ArrayList排序
System.out.println("排序后:");
System.out.println(pairList);
}
}

//继承Comparable接口排序该类是“可排序的”
//<>里面的是排序时与当前实例进行比较的实例的类型
//一般都和当前实例是同一个类型,比如这里就是Pair的实例和Pair的实例比较
classPairimplementsComparable<Pair>{

publicintleft;
publicintright;

publicPair(intleft,intright){
this.left=left;
this.right=right;
}

@Override
publicStringtoString(){
return"["+left+","+right+"]";
}

//排序规则,先按left排序,再按right排序
@Override
publicintcompareTo(Pairthat){
if(this.left>that.left){
return1;
}elif(this.left<that.left){
return-1;
}elif(this.right>that.right){
return1;
}elif(this.right<that.right){
return-1;
}
return0;
}

}

可以发现先按 left 排序,如果 left 相等,则按 right 排序


关于Java的ArrayList排序

1、这段代码是没问题的,我试过了。
2、“类型
List
中的方法
add(int,
Object)对于参数(int)不适用”,你是在什么地方看到的,java里好像没有中文信息。这句话没完全看懂,详细说明一下add方法的用法吧。
List的add(int
index,
Object
obj)方法是在指定的索引index的位置插入一个对象obj,原index处和其后的对象依次后移一位。指定的index最大可以是List.size(),也就是最多可以把obj放到List的最后。index

list.size()的时候会引发异常。
方法的第一个参数类型是int,第二个是Object。jdk1.4和更早的版本里,第二个参数必须是Object。add(0,1)这种写法是错误的,必须用add(0,new
Integer(1))。从jdk1.5开始,java加入一个重要的机制:自动拆装箱,简单的理解就是基本数据类型和其封装类的自动转化。现在add(0,1)这种写法是没有错误的。

如何对ArrayList中的某个属性进行排序

这个结果是降序的,升序的话条件换下
Collections.sort(list, new Comparator<Map<String, Object>>() {

@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
// TODO Auto-generated method stub
int time1 = (Integer) o1.get("RemindTime");
int time2 = (Integer) o2.get("RemindTime");
if (time1 > time2) {
return -1;
} el if (time1 == time2) {
return 0;
} el {
return 1;
}
}
});

本文发布于:2023-02-28 18:57:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/zhishi/a/167759106948117.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:arraylist排序(arraylist排序问题).doc

本文 PDF 下载地址:arraylist排序(arraylist排序问题).pdf

上一篇:系统字体
下一篇:返回列表
标签:arraylist
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 实用文体写作网旗下知识大全大全栏目是一个全百科类宝库! 优秀范文|法律文书|专利查询|