分享

【转】Java工具类

 小丑g22xft6chp 2017-04-09
  1. package com.luang.util.common;  
  2.   
  3.   
  4. import java.util.ArrayList;  
  5. import java.util.Arrays;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8. import java.util.Random;  
  9. import java.util.TreeMap;  
  10.   
  11.   
  12. /** 
  13.  *  
  14.  * ArrayUtil.java 
  15.  * 
  16.  * @desc 数组操作工具 
  17.  * @author Guoxp 
  18.  * @datatime Apr 7, 2013 4:03:49 PM 
  19.  * 
  20.  */  
  21. public class ArrayUtil {    
  22.     
  23.     /**  
  24.     * 排序算法的分类如下:  
  25.     * 1.插入排序(直接插入排序、折半插入排序、希尔排序);  
  26.     * 2.交换排序(冒泡泡排序、快速排序);  
  27.     * 3.选择排序(直接选择排序、堆排序);   
  28.     * 4.归并排序;   
  29.     * 5.基数排序。  
  30.     *  
  31.     * 关于排序方法的选择:  
  32.     * (1)若n较小(如n≤50),可采用直接插入或直接选择排序。  
  33.     * (2)若文件初始状态基本有序(指正序),则应选用直接插人、冒泡或随机的快速排序为宜;  
  34.     * (3)若n较大,则应采用时间复杂度为O(nlgn)的排序方法:快速排序、堆排序或归并排序。  
  35.     *  
  36.     */    
  37.         
  38.      /**  
  39.      * 交换数组中两元素  
  40.      *  
  41.      * @since 1.1  
  42.      * @param ints  
  43.      *            需要进行交换操作的数组  
  44.      * @param x  
  45.      *            数组中的位置1  
  46.      * @param y  
  47.      *            数组中的位置2  
  48.      * @return 交换后的数组  
  49.      */    
  50.     public static int[] swap(int[] ints, int x, int y) {    
  51.         int temp = ints[x];    
  52.         ints[x] = ints[y];    
  53.         ints[y] = temp;    
  54.         return ints;    
  55.     }    
  56.     
  57.     /**  
  58.      * 冒泡排序 方法:相邻两元素进行比较 性能:比较次数O(n^2),n^2/2;交换次数O(n^2),n^2/4  
  59.      *  
  60.      * @since 1.1  
  61.      * @param source  
  62.      *            需要进行排序操作的数组  
  63.      * @return 排序后的数组  
  64.      */    
  65.     public static int[] bubbleSort(int[] source) {    
  66.         for (int i = 1; i < source.length; i++) {    
  67.             for (int j = 0; j < i; j++) {    
  68.                 if (source[j] > source[j + 1]) {    
  69.                     swap(source, j, j + 1);    
  70.                 }    
  71.             }    
  72.         }    
  73.         return source;    
  74.     }    
  75.     
  76.     /**  
  77.      * 直接选择排序法 方法:每一趟从待排序的数据元素中选出最小(或最大)的一个元素, 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。  
  78.      * 性能:比较次数O(n^2),n^2/2 交换次数O(n),n  
  79.      * 交换次数比冒泡排序少多了,由于交换所需CPU时间比比较所需的CUP时间多,所以选择排序比冒泡排序快。  
  80.      * 但是N比较大时,比较所需的CPU时间占主要地位,所以这时的性能和冒泡排序差不太多,但毫无疑问肯定要快些。  
  81.      *  
  82.      * @since 1.1  
  83.      * @param source  
  84.      *            需要进行排序操作的数组  
  85.      * @return 排序后的数组  
  86.      */    
  87.     public static int[] selectSort(int[] source) {    
  88.     
  89.         for (int i = 0; i < source.length; i++) {    
  90.             for (int j = i + 1; j < source.length; j++) {    
  91.                 if (source[i] > source[j]) {    
  92.                     swap(source, i, j);    
  93.                 }    
  94.             }    
  95.         }    
  96.         return source;    
  97.     }    
  98.     
  99.     /**  
  100.      * 插入排序 方法:将一个记录插入到已排好序的有序表(有可能是空表)中,从而得到一个新的记录数增1的有序表。 性能:比较次数O(n^2),n^2/4  
  101.      * 复制次数O(n),n^2/4 比较次数是前两者的一般,而复制所需的CPU时间较交换少,所以性能上比冒泡排序提高一倍多,而比选择排序也要快。  
  102.      *  
  103.      * @since 1.1  
  104.      * @param source  
  105.      *            需要进行排序操作的数组  
  106.      * @return 排序后的数组  
  107.      */    
  108.     public static int[] insertSort(int[] source) {    
  109.     
  110.         for (int i = 1; i < source.length; i++) {    
  111.             for (int j = i; (j > 0) && (source[j] < source[j - 1]); j--) {    
  112.                 swap(source, j, j - 1);    
  113.             }    
  114.         }    
  115.         return source;    
  116.     }    
  117.     
  118.     /**  
  119.      * 快速排序 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists)。 步骤为:  
  120.      * 1. 从数列中挑出一个元素,称为 "基准"(pivot), 2.  
  121.      * 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面  
  122.      * (相同的数可以到任一边)。在这个分割之后,该基准是它的最后位置。这个称为分割(partition)操作。 3.  
  123.      * 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。  
  124.      * 递回的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了  
  125.      * 。虽然一直递回下去,但是这个算法总会结束,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。  
  126.      *  
  127.      * @since 1.1  
  128.      * @param source  
  129.      *            需要进行排序操作的数组  
  130.      * @return 排序后的数组  
  131.      */    
  132.     public static int[] quickSort(int[] source) {    
  133.         return qsort(source, 0, source.length - 1);    
  134.     }    
  135.     
  136.     /**  
  137.      * 快速排序的具体实现,排正序  
  138.      *  
  139.      * @since 1.1  
  140.      * @param source  
  141.      *            需要进行排序操作的数组  
  142.      * @param low  
  143.      *            开始低位  
  144.      * @param high  
  145.      *            结束高位  
  146.      * @return 排序后的数组  
  147.      */    
  148.     private static int[] qsort(int source[], int low, int high) {    
  149.         int i, j, x;    
  150.         if (low < high) {    
  151.             i = low;    
  152.             j = high;    
  153.             x = source[i];    
  154.             while (i < j) {    
  155.                 while (i < j && source[j] > x) {    
  156.                     j--;    
  157.                 }    
  158.                 if (i < j) {    
  159.                     source[i] = source[j];    
  160.                     i++;    
  161.                 }    
  162.                 while (i < j && source[i] < x) {    
  163.                     i++;    
  164.                 }    
  165.                 if (i < j) {    
  166.                     source[j] = source[i];    
  167.                     j--;    
  168.                 }    
  169.             }    
  170.             source[i] = x;    
  171.             qsort(source, low, i - 1);    
  172.             qsort(source, i + 1, high);    
  173.         }    
  174.         return source;    
  175.     }    
  176.     ///////////////////////////////////////////////    
  177.     //排序算法结束    
  178.     //////////////////////////////////////////////    
  179.     /**  
  180.      * 二分法查找 查找线性表必须是有序列表  
  181.      *  
  182.      * @since 1.1  
  183.      * @param source  
  184.      *            需要进行查找操作的数组  
  185.      * @param key  
  186.      *            需要查找的值  
  187.      * @return 需要查找的值在数组中的位置,若未查到则返回-1  
  188.      */    
  189.     public static int binarySearch(int[] source, int key) {    
  190.         int low = 0, high = source.length - 1, mid;    
  191.         while (low <= high) {    
  192.             mid = (low + high) >>> 1;    
  193.             if (key == source[mid]) {    
  194.                 return mid;    
  195.             } else if (key < source[mid]) {    
  196.                 high = mid - 1;    
  197.             } else {    
  198.                 low = mid + 1;    
  199.             }    
  200.         }    
  201.         return -1;    
  202.     }    
  203.     
  204.     /**  
  205.      * 反转数组  
  206.      *  
  207.      * @since 1.1  
  208.      * @param source  
  209.      *            需要进行反转操作的数组  
  210.      * @return 反转后的数组  
  211.      */    
  212.     public static int[] reverse(int[] source) {    
  213.         int length = source.length;    
  214.         int temp = 0;    
  215.         for (int i = 0; i < length>>1; i++) {    
  216.             temp = source[i];    
  217.             source[i] = source[length - 1 - i];    
  218.             source[length - 1 - i] = temp;    
  219.         }    
  220.         return source;    
  221.     }    
  222.    /**  
  223.     * 在当前位置插入一个元素,数组中原有元素向后移动;  
  224.     * 如果插入位置超出原数组,则抛IllegalArgumentException异常  
  225.     * @param array  
  226.     * @param index  
  227.     * @param insertNumber  
  228.     * @return  
  229.     */    
  230.     public static int[] insert(int[] array, int index, int insertNumber) {    
  231.         if (array == null || array.length == 0) {    
  232.             throw new IllegalArgumentException();    
  233.         }    
  234.         if (index-1 > array.length || index <= 0) {    
  235.             throw new IllegalArgumentException();    
  236.         }    
  237.         int[] dest=new int[array.length+1];    
  238.         System.arraycopy(array, 0, dest, 0, index-1);    
  239.         dest[index-1]=insertNumber;    
  240.         System.arraycopy(array, index-1, dest, index, dest.length-index);    
  241.         return dest;    
  242.     }    
  243.         
  244.     /**  
  245.      * 整形数组中特定位置删除掉一个元素,数组中原有元素向前移动;  
  246.      * 如果插入位置超出原数组,则抛IllegalArgumentException异常  
  247.      * @param array  
  248.      * @param index  
  249.      * @return  
  250.      */    
  251.     public static int[] remove(int[] array, int index) {    
  252.         if (array == null || array.length == 0) {    
  253.             throw new IllegalArgumentException();    
  254.         }    
  255.         if (index > array.length || index <= 0) {    
  256.             throw new IllegalArgumentException();    
  257.         }    
  258.         int[] dest=new int[array.length-1];    
  259.         System.arraycopy(array, 0, dest, 0, index-1);    
  260.         System.arraycopy(array, index, dest, index-1, array.length-index);    
  261.         return dest;    
  262.     }    
  263.     /**  
  264.      * 2个数组合并,形成一个新的数组  
  265.      * @param array1  
  266.      * @param array2  
  267.      * @return  
  268.      */    
  269.     public static int[] merge(int[] array1,int[] array2) {    
  270.         int[] dest=new int[array1.length+array2.length];    
  271.         System.arraycopy(array1, 0, dest, 0, array1.length);    
  272.         System.arraycopy(array2, 0, dest, array1.length, array2.length);    
  273.         return dest;    
  274.     }    
  275.     
  276. /**  
  277.      * 数组中有n个数据,要将它们顺序循环向后移动k位,  
  278.      * 即前面的元素向后移动k位,后面的元素则循环向前移k位,  
  279.      * 例如,0、1、2、3、4循环移动3位后为2、3、4、0、1。  
  280.      * @param array  
  281.      * @param offset  
  282.      * @return  
  283.      */    
  284.     public static int[] offsetArray(int[] array,int offset){    
  285.         int length = array.length;      
  286.         int moveLength = length - offset;     
  287.         int[] temp = Arrays.copyOfRange(array, moveLength, length);    
  288.         System.arraycopy(array, 0, array, offset, moveLength);      
  289.         System.arraycopy(temp, 0, array, 0, offset);    
  290.         return array;    
  291.     }    
  292.     /**  
  293.      * 随机打乱一个数组  
  294.      * @param list  
  295.      * @return  
  296.      */    
  297.     public static List shuffle(List list){    
  298.         java.util.Collections.shuffle(list);    
  299.         return list;    
  300.     }    
  301.     
  302.     /**  
  303.      * 随机打乱一个数组  
  304.      * @param array  
  305.      * @return  
  306.      */    
  307.     public int[] shuffle(int[] array) {    
  308.         Random random = new Random();    
  309.         for (int index = array.length - 1; index >= 0; index--) {    
  310.             // 从0到index处之间随机取一个值,跟index处的元素交换    
  311.             exchange(array, random.nextInt(index + 1), index);    
  312.         }    
  313.         return array;    
  314.     }    
  315.     
  316.     // 交换位置    
  317.     private void exchange(int[] array, int p1, int p2) {    
  318.         int temp = array[p1];    
  319.         array[p1] = array[p2];    
  320.         array[p2] = temp;    
  321.     }    
  322. /**  
  323.      * 对两个有序数组进行合并,并将重复的数字将其去掉  
  324.      *   
  325.      * @param a:已排好序的数组a  
  326.      * @param b:已排好序的数组b  
  327.      * @return 合并后的排序数组  
  328.      */    
  329.     private static List<Integer> mergeByList(int[] a, int[] b) {    
  330.         // 用于返回的新数组,长度可能不为a,b数组之和,因为可能有重复的数字需要去掉    
  331.         List<Integer> c = new ArrayList<Integer>();    
  332.         // a数组下标    
  333.         int aIndex = 0;    
  334.         // b数组下标    
  335.         int bIndex = 0;    
  336.         // 对a、b两数组的值进行比较,并将小的值加到c,并将该数组下标+1,    
  337.         // 如果相等,则将其任意一个加到c,两数组下标均+1    
  338.         // 如果下标超出该数组长度,则退出循环    
  339.         while (true) {    
  340.             if (aIndex > a.length - 1 || bIndex > b.length - 1) {    
  341.                 break;    
  342.             }    
  343.             if (a[aIndex] < b[bIndex]) {    
  344.                 c.add(a[aIndex]);    
  345.                 aIndex++;    
  346.             } else if (a[aIndex] > b[bIndex]) {    
  347.                 c.add(b[bIndex]);    
  348.                 bIndex++;    
  349.             } else {    
  350.                 c.add(a[aIndex]);    
  351.                 aIndex++;    
  352.                 bIndex++;    
  353.             }    
  354.         }    
  355.         // 将没有超出数组下标的数组其余全部加到数组c中    
  356.         // 如果a数组还有数字没有处理    
  357.         if (aIndex <= a.length - 1) {    
  358.             for (int i = aIndex; i <= a.length - 1; i++) {    
  359.                 c.add(a[i]);    
  360.             }    
  361.             // 如果b数组中还有数字没有处理    
  362.         } else if (bIndex <= b.length - 1) {    
  363.             for (int i = bIndex; i <= b.length - 1; i++) {    
  364.                 c.add(b[i]);    
  365.             }    
  366.         }    
  367.         return c;    
  368.     }    
  369.     /**  
  370.      * 对两个有序数组进行合并,并将重复的数字将其去掉  
  371.      * @param a:已排好序的数组a  
  372.      * @param b:已排好序的数组b  
  373.      * @return合并后的排序数组,返回数组的长度=a.length + b.length,不足部分补0  
  374.      */    
  375.     private static int[] mergeByArray(int[] a, int[] b){    
  376.         int[] c = new int[a.length + b.length];    
  377.     
  378.         int i = 0, j = 0, k = 0;    
  379.     
  380.         while (i < a.length && j < b.length) {    
  381.             if (a[i] <= b[j]) {    
  382.                 if (a[i] == b[j]) {    
  383.                     j++;    
  384.                 } else {    
  385.                     c[k] = a[i];    
  386.                     i++;    
  387.                     k++;    
  388.                 }    
  389.             } else {    
  390.                 c[k] = b[j];    
  391.                 j++;    
  392.                 k++;    
  393.             }    
  394.         }    
  395.         while (i < a.length) {    
  396.             c[k] = a[i];    
  397.             k++;    
  398.             i++;    
  399.         }    
  400.         while (j < b.length) {    
  401.             c[k] = b[j];    
  402.             j++;    
  403.             k++;    
  404.         }    
  405.         return c;    
  406.     }    
  407.     /**  
  408.      * 对两个有序数组进行合并,并将重复的数字将其去掉  
  409.      * @param a:可以是没有排序的数组  
  410.      * @param b:可以是没有排序的数组  
  411.      * @return合并后的排序数组  
  412.      * 打印时可以这样:  
  413.      * Map<Integer,Integer> map=sortByTreeMap(a,b);  
  414.         Iterator iterator =  map.entrySet().iterator();     
  415.         while (iterator.hasNext()) {     
  416.            Map.Entry mapentry = (Map.Entry)iterator.next();     
  417.            System.out.print(mapentry.getValue()+" ");     
  418.         }  
  419.      */    
  420.     private static Map<Integer,Integer> mergeByTreeMap(int[] a, int[] b) {    
  421.         Map<Integer,Integer> map=new TreeMap<Integer,Integer>();    
  422.         for(int i=0;i<a.length;i++){    
  423.             map.put(a[i], a[i]);    
  424.         }    
  425.         for(int i=0;i<b.length;i++){    
  426.             map.put(b[i], b[i]);    
  427.         }    
  428.         return map;    
  429.     }    
  430.     /**  
  431.      * 在控制台打印数组,之间用逗号隔开,调试时用  
  432.      * @param array  
  433.      */    
  434.     public static String print(int[] array){    
  435.         StringBuffer sb=new StringBuffer();    
  436.         for(int i=0;i<array.length;i++){    
  437.             sb.append(","+array[i]);    
  438.         }    
  439.         System.out.println(sb.toString().substring(1));    
  440.         return sb.toString().substring(1);    
  441.     }    
  442.     public static void main(String[] args){    
  443.         ArrayUtil util=new ArrayUtil();    
  444.         int[] array0={21,24,13,46,35,26,14,43,11};    
  445.         int[] array1={1,2,3,4,5,6};    
  446.         int[] array2={11,22,33,44,55,66};    
  447.         int[] temp=util.quickSort(array0);    
  448.         print(temp);    
  449.             
  450.     }    
  451. }    
转自【http://blog.csdn.net/guoxuepeng123/article/details/8797920】

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多