集册 Java实例教程 查找连续ArrayList条目的最小条目的索引

查找连续ArrayList条目的最小条目的索引

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

482
查找连续ArrayList条目的最小条目的索引


//package com.nowjava;

import java.util.ArrayList;

/* 
*来 自
 N o w J a v a . c o m - 时  代  Java
*/

public class Main {

    /**

     * finds the index of the smallest entry in a continuous span of ArrayList entries

     * @param arrayList  the ArrayList that you want to find entries for

     * @param first  the starting index of the searched entry span

     * @param last  the ending index of the searched entry span 

     * @return the index of the smallest entry found between $first and $last

     */

    public static <T extends Comparable<? super T>> int getIndexOfSmallest(

            ArrayList<T> arrayList, int first, int last) {

        if (arrayList.size() > 0) {

            int smallestIndex = first;

            T smallestEntry = arrayList.get(first);

            for (int i = first; i <= last; i++) {

                if (arrayList.get(i).compareTo(smallestEntry) == -1) {/**来自 N o w J a v a . c o m**/

                    smalle
展开阅读全文