自定义服装类,在GenericGroup中存放一批服装,并按服装的价格排序
提示:在GenericGroup中增加排序的方法:public void sort(Comparator<? super T> c),
调用Arrays中的public static <T> void sort(T[] a,
Comparator<? super T> c) 方法对其中的数组进行排序。
提示:在GenericGroup中增加排序的方法:public void sort(Comparator<? super T> c),
调用Arrays中的public static <T> void sort(T[] a,
Comparator<? super T> c) 方法对其中的数组进行排序。
解决方案 »
- java 窗体开发需要一个属性控件,求思路?
- list 排序问题,解决立即给分结贴
- 怎样把XML文件的内容读到 StringBuffer中来!!!!!!!
- 好多人支持eclipse工具,请问它到底好在哪里呀?跟Jcreator比较一下怎么样??
- 要做毕业设计了,做什么呀,大家给点意见
- 如何把java应用程序做成象exe文件那样可以一点就能直接执行的。
- Java入门的问题多多,请大侠多多指教!1.在Win98的Autoexec.bat中如何设置?(在任何路径下可以运行javac和java命令)2.java web start应
- 关于Applet,急!
- javamail 中subject 编码问题
- 如何将Ext正则表达式转换为Java正则表达式
- dbivs
- 各位大神,帮忙给个逻辑运算符用法的例子吧。
import java.util.Arrays;
import java.util.Comparator;public class GenericGroup { FuZhuang[] fzList; public void sort(Comparator<? super FuZhuang> c) {
Arrays.sort(fzList, c);
} public static void main(String[] args) {
new GenericGroup().sort(new FZComparator());
}}class FuZhuang {
int size; public FuZhuang(int size) {
super();
this.size = size;
}
}class FZComparator implements Comparator<FuZhuang> { @Override
public int compare(FuZhuang o1, FuZhuang o2) {
return o1.size - o2.size;
}}
{
String brand = null;
int price = 0;
public Clothing(String brand, int price)
{
this.brand = brand;
this.price = price;
}
@Override
public String toString()
{
return this.brand+"-"+this.price;
}
}class GenericGroup
{
public static void main(String[] args)
{
GenericGroup gc = new GenericGroup();
gc.sort(new Comparator<Clothing>(){
@Override
public int compare(Clothing c1, Clothing c2)
{
if(c1.price>c2.price)
{
return 1;
}
else if(c1.price<c2.price)
{
return -1;
}
return 0;
}
});
}
public void sort(Comparator<? super Clothing> c)
{
Clothing [] clothing = new Clothing[]{new Clothing("361",500),new Clothing("addi",2500),new Clothing("john",1500)};
Arrays.sort(clothing,c);
for(Clothing clo: clothing)
{
System.out.println(clo);
}
}}测试结果:361-500
john-1500
addi-2500