arraylist數(shù)組排序
用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);
}
});
}
如何實(shí)現(xiàn)對ArrayList排序 sort
使用Collections.sort()傳入ArrayList,會采用默認(rèn)的方式進(jìn)行排序(字典序)
使用Collections.sort()傳入ArrayList和自己實(shí)現(xiàn)Commparator接口的類的對象,實(shí)現(xiàn)自定義排序
使用List.sort()傳入自己實(shí)現(xiàn)Commparator接口的類的對象,實(shí)現(xiàn)自定義排序
Comparator返回值在jdk1.7、jdk1.8里必須是一對相反數(shù),如1和-1,不能是1和0.因?yàn)?.7的排序算法采用timsort,對返回值有嚴(yán)格要求
如何實(shí)現(xiàn)對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());
}
}
輸出結(jié)果:
按年齡升序:
張三 / 11
劉武 / 19
丁曉宇 / 21
趙四 / 22
按姓名排序:
丁曉宇 / 21
劉武 / 19
張三 / 11
趙四 / 22
java如何對Arraylist數(shù)組進(jìn)行排序(用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);//對數(shù)組排序
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接口排序該類是“可排序的”
//<>里面的是排序時(shí)與當(dāng)前實(shí)例進(jìn)行比較的實(shí)例的類型
//一般都和當(dāng)前實(shí)例是同一個(gè)類型,比如這里就是Pair的實(shí)例和Pair的實(shí)例比較
classPairimplementsComparable<Pair>{
publicintleft;
publicintright;
publicPair(intleft,intright){
this.left=left;
this.right=right;
}
@Override
publicStringtoString(){
return"["+left+","+right+"]";
}
//排序規(guī)則,先按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;
}
}
可以發(fā)現(xiàn)先按 left 排序,如果 left 相等,則按 right 排序
關(guān)于Java的ArrayList排序
1、這段代碼是沒問題的,我試過了。
2、“類型
List
中的方法
add(int,
Object)對于參數(shù)(int)不適用”,你是在什么地方看到的,java里好像沒有中文信息。這句話沒完全看懂,詳細(xì)說明一下add方法的用法吧。
List的add(int
index,
Object
obj)方法是在指定的索引index的位置插入一個(gè)對象obj,原index處和其后的對象依次后移一位。指定的index最大可以是List.size(),也就是最多可以把obj放到List的最后。index
〉
list.size()的時(shí)候會引發(fā)異常。
方法的第一個(gè)參數(shù)類型是int,第二個(gè)是Object。jdk1.4和更早的版本里,第二個(gè)參數(shù)必須是Object。add(0,1)這種寫法是錯(cuò)誤的,必須用add(0,new
Integer(1))。從jdk1.5開始,java加入一個(gè)重要的機(jī)制:自動拆裝箱,簡單的理解就是基本數(shù)據(jù)類型和其封裝類的自動轉(zhuǎn)化?,F(xiàn)在add(0,1)這種寫法是沒有錯(cuò)誤的。
如何對ArrayList中的某個(gè)屬性進(jìn)行排序
這個(gè)結(jié)果是降序的,升序的話條件換下
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;
}
}
});