程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java完成對中文字符串的排序功效實例代碼

Java完成對中文字符串的排序功效實例代碼

編輯:關於JAVA

Java完成對中文字符串的排序功效實例代碼。本站提示廣大學習愛好者:(Java完成對中文字符串的排序功效實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完成對中文字符串的排序功效實例代碼正文


空話不多說了,直接給年夜家代碼分享代碼了。

詳細代碼以下所示:

package test;
/**
* 
* @Title 書的信息類
* @author LR
* @version .
* @since --
*/
public class Book {
private String book_id;
private String book_name;
private String publishing_house;
public Book(String book_id, String book_name, String publishing_house) {
super();
this.book_id = book_id;
this.book_name = book_name;
this.publishing_house = publishing_house;
}
public String getBook_id() {
return book_id;
}
public void setBook_id(String book_id) {
this.book_id = book_id;
}
public String getBook_name() {
return book_name;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public String getPublishing_house() {
return publishing_house;
}
public void setPublishing_house(String publishing_house) {
this.publishing_house = publishing_house;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "書號"+book_id+"\n書名"+book_name+"\n出書社"+publishing_house;
}
}

package test;

import java.text.Collator;
/**
* 
* @Title 中文字符串排序功效
* @author LR
* @version .
* @since --
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CollectionChineseSort implements Comparator<Book>{
Collator collator= Collator.getInstance(java.util.Locale.CHINA);
public static void main(String[] args) {
ArrayList<Book> list=new ArrayList<Book>();
list.add(new Book("","英語","英語出書社"));
list.add(new Book("","日語","日語出書社"));
list.add(new Book("","德語","德語出書社"));
list.add(new Book("","法語","法語出書社"));
list.add(new Book("","俄語","俄語出書社"));
Collections.sort(list,new CollectionChineseSort());
for (Book book:list){ 
System.out.println(book); 
} 
}
@Override
public int compare(Book book, Book book) {
// TODO Auto-generated method stub
int compare_value=collator.compare(book.getBook_name(),book.getBook_name());
if(compare_value>){
return ;
}
if(compare_value<){
return -;
}
return ;
}
}

以上內容是小編給年夜家引見的Java完成中文字符串的排序功效的代碼,願望對年夜家有所贊助!

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