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 < 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 < 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]) > 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; } } } }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/11787.html