1.類實現comparable<>介面
public class book implements comparable
public void setbookname(string bookname)
public double getunitprice()
public void setunitprice(double unitprice)
public book(string bookname, double unitprice)
public book()
//重寫compareto方法,按照unitprice公升序排序,降序的話將兩個返回值互換即可
@override
public int compareto(book book)
return 1;
} //重寫tostring方法,輸出book屬性
@override
public string tostring()
}public class collectionsdemo
public void printinfo(listbooks)
}}
2.collections.sort(list,new comparator() {}
public int compare(double o1, double o2) 重寫方法
public class collectionsdemo
//按照unitprice公升序排序,降序的話將兩個返回值互換即可
collections.sort(liststr,new comparator()
});system.out.println("排序後");
for(double str:liststr)
}}
java 集合框架 之 list
list 介面 常見實現類 arraylist是基於陣列實現的,是乙個陣列佇列。可以動態的增加容量!查詢快 1.增加乙個值 list nums new arraylist nums.add 3 nums.add 5 nums.add 1 nums.add 0 system.out.println n...
Java基礎學習之集合 List
1.addall removeall retainall collection c1 new arraylist c1.add a c1.add b c1.add c c1.add d collection c2 new arraylist c2.add a c2.add b c2.add c c2...
集合 List 排序
以下是我做的乙個例項 第一步 建立物件類 content public class content public string getname public void setname string name public long gettime public void settime long t...