亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

JAVA認(rèn)證

Java編程中如何實現(xiàn)中文排序

時間:2024-07-19 00:15:01 JAVA認(rèn)證 我要投稿
  • 相關(guān)推薦

Java編程中如何實現(xiàn)中文排序

  在不斷的Java學(xué)習(xí)中,我們就能發(fā)現(xiàn)在很多的時候還是有不少的技巧。下面YJBYS小編為大家整理了關(guān)于Java編程中如何實現(xiàn)中文排序的文章,希望對你有所幫助。

  第一種情況:

  Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);

  String[] arr = { “張三”, “李四”, “王五”, “劉六” };

  Arrays.sort(arr, cmp);

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

  System.out.println(arr[i]);

  第二種情況:

  //ComparableBean.java

  import java.text.CollationKey;

  import java.text.Collator;

  import java.text.RuleBasedCollator;

  import java.util.Comparator;

  public class ComparableBean{

  private String name;

  public ComparableBean(String name) {

  this.name = name;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this.name = name;

  }

  }

  class ComparableBeanComparator implements Comparator//

  {

  RuleBasedCollator collator; // you can set your rules for the instance “collator”

  public ComparableBeanComparator()

  {

  collator = (RuleBasedCollator)Collator.getInstance(java.util.Locale.CHINA);// try testing various locales

  }

  public int compare(Object obj1, Object obj2) {

  String tempname1 = ((ComparableBean) obj1)。getName();

  String tempname2 = ((ComparableBean) obj2)。getName();

  CollationKey c1 = collator.getCollationKey(tempname1);

  CollationKey c2 = collator.getCollationKey(tempname2);

  // return collator.compare(((CollationKey) c1)。getSourceString(),

  // ((CollationKey) c2)。getSourceString());

  return collator.compare(((CollationKey) c2)。getSourceString(),

  ((CollationKey) c1)。getSourceString());

  }

  // public int compare(ComparableBean obj1, ComparableBean obj2) {

  // String tempname1 = obj1.getName();

  // String tempname2 = obj2.getName();

  //

  // CollationKey c1 = collator.getCollationKey(tempname1);

  // CollationKey c2 = collator.getCollationKey(tempname2);

  // return collator.compare(((CollationKey) c1)。getSourceString(),

  // ((CollationKey) c2)。getSourceString());

  // }

  }

  //the end of ComparableBean.java

  測試代碼:

  ComparableBean[] nameContent = { new ComparableBean(“一切從實際出發(fā)”),

  new ComparableBean(“立于不敗之地”), new ComparableBean(“多項式”),

  new ComparableBean(“貫徹落實”), new ComparableBean(“密切聯(lián)系群眾”),

  new ComparableBean(“四項基本原則”), new ComparableBean(“咬牙切齒”),

  new ComparableBean(“恭恭敬敬”), new ComparableBean(“民警”),

  new ComparableBean(“經(jīng)營承包責(zé)任制”) };

  Arrays.sort(nameContent,new ComparableBeanComparator());

  for (int i = 0; i 《 nameContent.length; i++) {

  System.out.println(nameContent[i]。getName());

  }

【Java編程中如何實現(xiàn)中文排序】相關(guān)文章:

Java如何實現(xiàn)點的在線添加08-31

word表格中的數(shù)據(jù)如何排序07-10

關(guān)于JAVA的XML編程09-03

Java基本編程技巧09-03

PHP中多態(tài)如何實現(xiàn)呢10-29

在word中如何實現(xiàn)自動續(xù)表06-23

java程序中如何調(diào)用linux命令06-13

Java中如何構(gòu)造、生成XML簡明09-02

關(guān)于JAVA實現(xiàn)httpClient的實例11-05

Java多線程的實現(xiàn)方式11-03