⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collections.java

📁 This is a resource based on j2me embedded,if you dont understand,you can connection with me .
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	    if (cmp < 0)		low = mid + 1;	    else if (cmp > 0)		high = mid - 1;	    else		return mid; // key found	}	return -(low + 1);  // key not found    }    private static int iteratorBinarySearch(List l, Object key, Comparator c) {	int low = 0;	int high = l.size()-1;        ListIterator i = l.listIterator();        while (low <= high) {            int mid = (low + high) >> 1;            Object midVal = get(i, mid);            int cmp = c.compare(midVal, key);            if (cmp < 0)                low = mid + 1;            else if (cmp > 0)                high = mid - 1;            else                return mid; // key found        }        return -(low + 1);  // key not found    }    /**     * 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 randomenss 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) {        list.set(i, list.set(j, list.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;    }    /**     * Replaces all of the elements of the specified list with the specified     * element. <p>     *     * This method runs in linear time.     *     * @param  list the list to be filled with the specified element.     * @param  obj The element with which to fill the specified list.     * @throws UnsupportedOperationException if the specified list or its     *	       list-iterator does not support the <tt>set</tt> operation.     */    public static void fill(List list, Object obj) {        int size = list.size();        if (size < FILL_THRESHOLD || list instanceof RandomAccess) {            for (int i=0; i<size; i++)                list.set(i, obj);        } else {            ListIterator itr = list.listIterator();            for (int i=0; i<size; i++) {                itr.next();                itr.set(obj);            }        }    }    /**     * Copies all of the elements from one list into another.  After the     * operation, the index of each copied element in the destination list     * will be identical to its index in the source list.  The destination     * list must be at least as long as the source list.  If it is longer, the     * remaining elements in the destination list are unaffected. <p>     *     * This method runs in linear time.     *     * @param  dest The destination list.     * @param  src The source list.     * @throws IndexOutOfBoundsException if the destination list is too small     *         to contain the entire source List.     * @throws UnsupportedOperationException if the destination list's     *         list-iterator does not support the <tt>set</tt> operation.     */    public static void copy(List dest, List src) {        int srcSize = src.size();        if (srcSize > dest.size())            throw new IndexOutOfBoundsException("Source does not fit in dest");        if (srcSize < COPY_THRESHOLD ||            (src instanceof RandomAccess && dest instanceof RandomAccess)) {            for (int i=0; i<srcSize; i++)                dest.set(i, src.get(i));        } else {            ListIterator di=dest.listIterator(), si=src.listIterator();            for (int i=0; i<srcSize; i++) {                di.next();                di.set(si.next());            }        }    }    /**     * Returns the minimum element of the given collection, according to the     * <i>natural ordering</i> of its elements.  All elements in the     * collection must implement the <tt>Comparable</tt> interface.     * Furthermore, all elements in the collection must be <i>mutually     * comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and     * <tt>e2</tt> in the collection).<p>     *     * This method iterates over the entire collection, hence it requires     * time proportional to the size of the collection.     *     * @param  coll the collection whose minimum element is to be determined.     * @return the minimum element of the given collection, according     *         to the <i>natural ordering</i> of its elements.     * @throws ClassCastException if the collection contains elements that are     *	       not <i>mutually comparable</i> (for example, strings and     *	       integers).     * @throws NoSuchElementException if the collection is empty.     * @see Comparable     */    public static Object min(Collection coll) {	Iterator i = coll.iterator();	Comparable candidate = (Comparable)(i.next());        while(i.hasNext()) {	    Comparable next = (Comparable)(i.next());	    if (next.compareTo(candidate) < 0)		candidate = next;	}	return candidate;    }    /**     * Returns the minimum element of the given collection, according to the     * order induced by the specified comparator.  All elements in the     * collection must be <i>mutually comparable</i> by the specified     * comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and     * <tt>e2</tt> in the collection).<p>     *     * This method iterates over the entire collection, hence it requires     * time proportional to the size of the collection.     *     * @param  coll the collection whose minimum element is to be determined.     * @param  comp the comparator with which to determine the minimum element.     *         A <tt>null</tt> value indicates that the elements' <i>natural     *         ordering</i> should be used.     * @return the minimum element of the given collection, according     *         to the specified comparator.     * @throws ClassCastException if the collection contains elements that are     *	       not <i>mutually comparable</i> using the specified comparator.     * @throws NoSuchElementException if the collection is empty.     * @see Comparable     */    public static Object min(Collection coll, Comparator comp) {        if (comp==null)            return min(coll);	Iterator i = coll.iterator();	Object candidate = i.next();        while(i.hasNext()) {	    Object next = i.next();	    if (comp.compare(next, candidate) < 0)		candidate = next;	}	return candidate;    }    /**     * Returns the maximum element of the given collection, according to the     * <i>natural ordering</i> of its elements.  All elements in the     * collection must implement the <tt>Comparable</tt> interface.     * Furthermore, all elements in the collection must be <i>mutually     * comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and     * <tt>e2</tt> in the collection).<p>     *     * This method iterates over the entire collection, hence it requires     * time proportional to the size of the collection.     *     * @param  coll the collection whose maximum element is to be determined.     * @return the maximum element of the given collection, according     *         to the <i>natural ordering</i> of its elements.     * @throws ClassCastException if the collection contains elements that are     *	       not <i>mutually comparable</i> (for example, strings and     *         integers).     * @throws NoSuchElementException if the collection is empty.     * @see Comparable     */    public static Object max(Collection coll) {	Iterator i = coll.iterator();	Comparable candidate = (Comparable)(i.next());        while(i.hasNext()) {	    Comparable next = (Comparable)(i.next());	    if (next.compareTo(candidate) > 0)		candidate = next;	}	return candidate;    }    /**     * Returns the maximum element of the given collection, according to the     * order induced by the specified comparator.  All elements in the     * collection must be <i>mutually comparable</i> by the specified     * comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and     * <tt>e2</tt> in the collection).<p>     *     * This method iterates over the entire collection, hence it requires     * time proportional to the size of the collection.     *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -