程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java實現集合,文件,數組排序

Java實現集合,文件,數組排序

編輯:關於JAVA

System.out.print("before sort: ");

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

System.out.print(arr[i] + " ");

System.out.println();

Arrays.sort(arr);

System.out.print("after sort: ");

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

System.out.print(arr[i] + " ");

System.out.println();

輸出結果:

before sort: 2 3 1 10 7 4

after sort: 1 2 3 4 7 10

我們看到排序結果是按照升序排列的,下面的排序都是如此。

對象的排序

對象可以放在數組裡,同樣調用Arrays.sort(Object[] arr)即可;也可以放到集裡,用java.util.Collections的sort(List list)。

但是這個類必須實現了java.lang.Comparable接口。這個接口只有一個方法:int compartTo(Object o),

當本對象比傳入的對象大時,返回一個正整數。 以類Programmer為例:

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 + "}";

}

}

對其進行排序:

ArrayList list = new ArrayList();

list.add(new Programmer("張三", "C", 12000));

list.add(new Programmer("李四", "Java", 200));

list.add(new Programmer("王五", "C++", 5000));

list.add(new Programmer("錢六", "VB", 3000));

System.out.println("before sort: " + list);

Collections.sort(list);

System.out.println("after sort: " + list);

輸出:

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}]

夠簡單吧!查查Comparable的javadoc可以知道,有很多類已經實現了該接口,因此對這些類的排序幾行代碼就可以搞定。

最近看C#發現其中用System.Array.sort對數組排序,適用於所有實現了IComparable接口的對象,看來微軟的借鑒能力還真是強啊!

對已有類進行排序

上面的方法有一個問題,就是一個類已經存在了,並且沒有實現Comparable接口,使用一個子類進行封裝?

很麻煩(你可以對下面的例子試試)。還有一種情況就是對一個類沒法實現多種排序。以File類為例,它實現了Comparable接口,

但是是按照名稱排序的。如果要按照大小排序,或者按修改時間排序呢?對這兩種情況,使用java.util包的Comparator接口:

Arrays.sort(Object[] arr, Comparator com)

Collections.sort(Object[] arr, Comparator com)

Comparator接口的方法:

public int compare(Object o1, Object o2) 當o1比o2大時返回一個正整數

public boolean equals(Object obj) 判斷obj與這個Comparator是否同一個對象

下面使用Comparator對文件實現了按文件大小或修改時間排序:

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; //簡單做法

}

}

}

調用的示例:

File dir = new File("C:\\temp");

File[] files = dir.listFiles();

System.out.print("before sort: ");

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

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

System.out.println();

Arrays.sort(files);

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

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

System.out.print(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.print(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.print(files[i] + " ");

System.out.println();

自己找個目錄試一下吧。用這些Java類庫中的方法,一般情況下應該是不用自己寫排序算法了吧?

最後附上完整代碼占點版面:

import java.io.*;

import java.util.*;

public class TestSort {

public static void main(String[] args) {

sortSimpleType();

sortComparable();

sortComparator();

}

public static void sortSimpleType() {

int[] arr = {2, 3, 1,10,7,4};

System.out.print("before sort: ");

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

System.out.print(arr[i] + " ");

System.out.println();

Arrays.sort(arr);

System.out.print("after sort: ");

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

System.out.print(arr[i] + " ");

System.out.println();

}

public static void sortComparable() {

ArrayList list = new ArrayList();

list.add(new Programmer("張三", "C", 12000));

list.add(new Programmer("李四", "Java", 200));

list.add(new Programmer("王五", "C++", 5000));

list.add(new Programmer("錢六", "VB", 3000));

System.out.println("before sort: " + list);

Collections.sort(list);

System.out.println("after sort: " + list);

}

public static void sortComparator() {

File dir = new File("C:\\java");

File[] files = dir.listFiles();

System.out.print("before sort: ");

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

System.out.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; //簡單做法

}

}

}

運行結果:

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\FileUtils$CompratorByLastModified.class

C:\java\FileUtils$CompratorBySize.class

C:\java\FileUtils.class

C:\java\hib.bat

C:\java\hjar

C:\java\jar

C:\java\jsf.bat

C:\java\lib

C:\java\lib2

C:\java\messages.properties

C:\java\messages_zh_TW.properties

C:\java\messages_zh_TW1.properties

C:\java\native.bat

C:\java\onlyfun

C:\java\Programmer.class

C:\java\src

C:\java\struts.bat

C:\java\TestSort.class

C:\java\TestSort.java

C:\java\xdoclet

sort by size: C:\java\hjar

C:\java\jar

C:\java\lib

C:\java\lib2

C:\java\onlyfun

C:\java\src

C:\java\xdoclet

C:\java\1.txt

C:\java\native.bat

C:\java\jsf.bat

C:\java\struts.bat

C:\java\class.bat

C:\java\FileUtils.class

C:\java\hib.bat

C:\java\messages.properties

C:\java\FileUtils$CompratorBySize.class

C:\java\FileUtils$CompratorByLastModified.class

C:\java\bean.xml

C:\java\Programmer.class

C:\java\messages_zh_TW.properties

C:\java\messages_zh_TW1.properties

C:\java\build.xml

C:\java\TestSort.class

C:\java\TestSort.java

sort by last modified: C:\java\lib

C:\java\jar

C:\java\hjar

C:\java\onlyfun

C:\java\1.txt

C:\java\hib.bat

C:\java\src

C:\java\xdoclet

C:\java\build.xml

C:\java\lib2

C:\java\bean.xml

C:\java\class.bat

C:\java\struts.bat

C:\java\messages.properties

C:\java\messages_zh_TW1.properties

C:\java\messages_zh_TW.properties

C:\java\jsf.bat

C:\java\native.bat

C:\java\TestSort.java

C:\java\FileUtils.class

C:\java\FileUtils$CompratorBySize.class

C:\java\FileUtils$CompratorByLastModified.class

C:\java\Programmer.class

C:\java\TestSort.class

C:\java>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved