finds the index of the smallest entry in a continuous span of ArrayList entries - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

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

Demo Code


//package com.java2s;
import java.util.ArrayList;

public class Main {
    /**/*from w  ww .j ava 2  s . c o  m*/
     * 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) {
                    smallestIndex = i;
                    smallestEntry = arrayList.get(i);
                }
            }
            return smallestIndex;
        } else
            return -1;
    }
}

Related Tutorials