当前位置:网站首页 > Java基础 > 正文

java定制排序教程



import java.util.Arrays; import java.util.Comparator;

public class ArrarysCommonMethods {

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 默认实现如何实现定制排序的 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用冒泡法完成</span> <span style="color: rgba(0, 0, 255, 1)">int</span> myarr[] = { 1, -1, 4, 3, 9, -2, 8<span style="color: rgba(0, 0, 0, 1)"> }; MyArrys.sort(myarr, </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MyComparator() { @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> compare(<span style="color: rgba(0, 0, 255, 1)">int</span> n1, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> n2) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> n2-<span style="color: rgba(0, 0, 0, 1)">n1; } }); System.out.println(</span>"自己使用冒泡写的定制排序:" +<span style="color: rgba(0, 0, 0, 1)"> Arrays.toString(myarr)); } 

}

interface MyComparator {

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> compare(<span style="color: rgba(0, 0, 255, 1)">int</span> n1, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> n2); 

}

class MyArrys {

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 静态方法</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> sort(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[] arr, MyComparator comparator) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 冒泡</span> <span style="color: rgba(0, 0, 255, 1)">int</span> temp = 0<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; arr.length - 1; i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> j = 0; j &lt; arr.length - 1 - i; j++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (comparator.compare(arr[j], arr[j + 1]) &gt; 0<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 交换</span> temp =<span style="color: rgba(0, 0, 0, 1)"> arr[j]; arr[j] </span>= arr[j + 1<span style="color: rgba(0, 0, 0, 1)">]; arr[j </span>+ 1] =<span style="color: rgba(0, 0, 0, 1)"> temp; } } } } 

}

  • 上一篇: java任务调度器教程
  • 下一篇: java铺路教程
  • 版权声明


    相关文章:

  • java任务调度器教程2025-11-29 17:10:01
  • java dom教程2025-11-29 17:10:01
  • java开放安装教程2025-11-29 17:10:01
  • 455集java教程2025-11-29 17:10:01
  • java谁的教程2025-11-29 17:10:01
  • java铺路教程2025-11-29 17:10:01
  • java直播功能教程2025-11-29 17:10:01
  • java教程2482025-11-29 17:10:01
  • java教程自学网壁虎2025-11-29 17:10:01
  • java掠夺塔改造教程2025-11-29 17:10:01