資料結構 java 整理

來源:互聯網
上載者:User
都是取之於JDK源碼或者 apache、 google jar 包源碼 

  /**

     * Reverses the order of the elements in the specified list.<p>     *     * This method runs in linear time.     *     * @param  list the list whose elements are to be reversed.     * @throws UnsupportedOperationException if the specified list or     *         its list-iterator does not support the <tt>set</tt> method.     */    public static void reverse(List<?> list) {        int size = list.size();        if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {            for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)                swap(list, i, j);        } else {            ListIterator fwd = list.listIterator();            ListIterator rev = list.listIterator(size);            for (int i=0, mid=list.size()>>1; i<mid; i++) {                Object tmp = fwd.next();                fwd.set(rev.previous());                rev.set(tmp);            }        }    }     /**     * Randomly permutes the specified list using a default source of     * randomness.  All permutations occur with approximately equal     * likelihood.<p>     *     * The hedge "approximately" is used in the foregoing description because     * default source of randomness is only approximately an unbiased source     * of independently chosen bits. If it were a perfect source of randomly     * chosen bits, then the algorithm would choose permutations with perfect     * uniformity.<p>     *     * This implementation traverses the list backwards, from the last element     * up to the second, repeatedly swapping a randomly selected element into     * the "current position".  Elements are randomly selected from the     * portion of the list that runs from the first element to the current     * position, inclusive.<p>     *     * This method runs in linear time.  If the specified list does not     * implement the {@link RandomAccess} interface and is large, this     * implementation dumps the specified list into an array before shuffling     * it, and dumps the shuffled array back into the list.  This avoids the     * quadratic behavior that would result from shuffling a "sequential     * access" list in place.     *     * @param  list the list to be shuffled.     * @throws UnsupportedOperationException if the specified list or     *         its list-iterator does not support the <tt>set</tt> method.     */    public static void shuffle(List<?> list) {        shuffle(list, r);    }     private static Random r = new Random();     /**     * Randomly permute the specified list using the specified source of     * randomness.  All permutations occur with equal likelihood     * assuming that the source of randomness is fair.<p>     *     * This implementation traverses the list backwards, from the last element     * up to the second, repeatedly swapping a randomly selected element into     * the "current position".  Elements are randomly selected from the     * portion of the list that runs from the first element to the current     * position, inclusive.<p>     *     * This method runs in linear time.  If the specified list does not     * implement the {@link RandomAccess} interface and is large, this     * implementation dumps the specified list into an array before shuffling     * it, and dumps the shuffled array back into the list.  This avoids the     * quadratic behavior that would result from shuffling a "sequential     * access" list in place.     *     * @param  list the list to be shuffled.     * @param  rnd the source of randomness to use to shuffle the list.     * @throws UnsupportedOperationException if the specified list or its     *         list-iterator does not support the <tt>set</tt> operation.     */    public static void shuffle(List<?> list, Random rnd) {        int size = list.size();        if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {            for (int i=size; i>1; i--)                swap(list, i-1, rnd.nextInt(i));        } else {            Object arr[] = list.toArray();            // Shuffle array            for (int i=size; i>1; i--)                swap(arr, i-1, rnd.nextInt(i));            // Dump array back into list            ListIterator it = list.listIterator();            for (int i=0; i<arr.length; i++) {                it.next();                it.set(arr[i]);            }        }    }     /**     * Swaps the elements at the specified positions in the specified list.     * (If the specified positions are equal, invoking this method leaves     * the list unchanged.)     *     * @param list The list in which to swap elements.     * @param i the index of one element to be swapped.     * @param j the index of the other element to be swapped.     * @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt>     *         is out of range (i &lt; 0 || i &gt;= list.size()     *         || j &lt; 0 || j &gt;= list.size()).     * @since 1.4     */    public static void swap(List<?> list, int i, int j) {        final List l = list;        l.set(i, l.set(j, l.get(i)));    }    /**     * Swaps the two specified elements in the specified array.     */    private static void swap(Object[] arr, int i, int j) {        Object tmp = arr[i];        arr[i] = arr[j];        arr[j] = tmp;    }  .. 待續

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.