快速业务通道

Java实现集合,文件,数组排序 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-16
.println(files[i] + " ");

System.out.println();

Arrays.sort(files);

System.out.print("sort by name: ");

for (int i = 0; i< files.length; i++)

System.out.println(files[i] + " ");

System.out.println();

Arrays.sort(files, new FileUtils.CompratorBySize());

System.out.print("sort by size: ");

for (int i = 0; i< files.length; i++)

System.out.println(files[i] + " ");

System.out.println();

Arrays.sort(files, new FileUtils.CompratorByLastModified());

System.out.print("sort by last modified: ");

for (int i = 0; i< files.length; i++)

System.out.println(files[i] + " ");

System.out.println();

}

}

class Programmer implements Comparable{

private String name;

private String language;

private double pay;

public Programmer(String name, String language, double pay) {

this.name = name;

this.language = language;

this.pay = pay;

}

public int compareTo(Object o) {

Programmer other = (Programmer)o;

return (int)pay - (int)other.pay;

}

public String toString(){

return "{name: " + name + ", language: " + language + ", money: " + pay + "}";

}

}

class FileUtils {

static class CompratorByLastModified implements Comparator {

public int compare(Object o1, Object o2) {

File file1 = (File)o1;

File file2 = (File)o2;

long diff = file1.lastModified() - file2.lastModified();

if (diff > 0)

return 1;

else if (diff == 0)

return 0;

else

return -1;

}

public boolean equals(Object obj){

return true; //简单做法

}

}

static class CompratorBySize implements Comparator {

public int compare(Object o1, Object o2) {

File file1 = (File)o1;

File file2 = (File)o2;

long diff = file1.length() - file2.length();

if (diff > 0)

return 1;

else if (diff == 0)

return 0;

else

return -1;

}

public boolean equals(Object obj){

return true; //简单做法

}

}

}

Java实现集合,文件,数组排序(4)

时间:2011-06-19 ITeye mx19841031

运行结果:

C:\java>java TestSort

before sort: 2 3 1 10 7 4

after sort: 1 2 3 4 7 10

before sort: [{name: 张三, language: C, money: 12000.0}, {name: 李四, language:

Java, money: 200.0}, {name: 王五, language: C++, money: 5000.0}, {name: 钱六, language: VB, money: 3000.0}]

after sort: [{name: 李四, language: Java, money: 200.0}, {name: 钱六, language:

VB, money: 3000.0}, {name: 王五, language: C++, money: 5000.0}, {name: 张三, language: C, money: 12000.0}]

before sort: C:\java\class.bat

C:\java\onlyfun

C:\java\build.xml

C:\java\1.txt

C:\java\native.bat

C:\java\hib.bat

C:\java\hjar

C:\java\src

C:\java\xdoclet

C:\java\lib2

C:\java\jsf.bat

C:\java\struts.bat

C:\java\TestSort.java

C:\java\TestSort.class

C:\java\jar

C:\java\messages_zh_TW1.properties

C:\java\messages.properties

C:\java\messages_zh_TW.properties

C:\java\Programmer.class

C:\java\FileUtils$CompratorByLastModified.class

C:\java\FileUtils$CompratorBySize.class

C:\java\FileUtils.class

C:\java\lib

C:\java\bean.xml

sort by name: C:\java\1.txt

C:\java\bean.xml

C:\java\build.xml

C:\java\class.bat

C:\java\FileU

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号