集册 Java实例教程 在指定的索引处排列ArrayList的三个条目时,假定左<=中<=右

在指定的索引处排列ArrayList的三个条目时,假定左<=中<=右

欢马劈雪     最近更新时间:2020-01-02 10:19:05

432
在指定的索引处排列ArrayList的三个条目时,假定左<=中<=右


import java.util.ArrayList;// 来自 N o w J a v a . c o m


public class Main{

    /**

     * orders three entries of an ArrayList at their specified indices

     * assumes left <= mid <= right

     * 

     * @param arrayList the ArrayList whose three entries you want to sort

     * @param left the first of the three indices 

     * @param mid the second of the three indices

     * @param right the last of the three indices

     */

    public static <T extends Comparable<? super T>> void orderThreeEntries(

            ArrayList<T> arrayList, int left, int mid, int right) {

        if (arrayList.get(left).compareTo(arrayList.get(mid)) == 1) {

            ArrayListUtil.swap(arrayList, left, mid);

        }

        if (arrayList.get(mid).compareTo(arrayList.get(right)) == 1) {

            ArrayListUtil.swap(arrayList, mid, right);

        }/** 时   代    Java - nowjava.com 提 供 **/

        if (arrayList.get(left).compareTo(arrayList.get(mid)) == 1) {

            ArrayListUtil.swap(arrayList, left, mid);

        }

    }

    /**

     * swaps two entries of an ArrayList

     * 

     * @param arrayList  the ArrayList whose entries you want to swap

     * @param yin  the index of the first swapped entry

     * @param yang  the index of the other swapped entry

     */

    public static <T extends 
展开阅读全文