第一種方法,就是list中物件的類定義裡實現comparable介面,再通過collectons.sort呼叫,**如下:
public class person implements comparable
public void setname(string name)
public integer getorder()
public void setorder(integer order)
@override
public int compareto(person arg0)
}
public static void main(string args)
}
第二種方法,過載collections.sort方法,通過乙個匿名內部類實現乙個compare()實現排序
public class person
public void setname(string name)
public integer getorder()
public void setorder(integer order)
}
public static void main(string args)
});for (person p : lista)
}
java學習日記 06 容器中的List
新增元素 arraylist al new arraylist al.add hello 1 al.add hello 2 al.add hello 3 追加乙個arraylist al.addall hello 3 刪除元素 al.remove 2 遍歷 普通迭代器iterator和list迭代器...
Java之List集合排序
1.類實現comparable 介面 public class book implements comparable public void setbookname string bookname public double getunitprice public void setunitprice...
Java基礎系列 List排序
public static super t void sort listlist public static void sort listlist,comparator c 複製 public class objectsort class studentcomparator implements c...