Returns whether an list index is in the range of valid indexes. - Android java.util

Android examples for java.util:List

Description

Returns whether an list index is in the range of valid indexes.

Demo Code


//package com.book2s;

import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "book2s.com");
        int index = 42;
        System.out.println(isValidIndex(list, index));
    }// w  ww  .  j  a  v  a2 s . c om

    /**
     * Returns whether an list index is in the range of valid indexes.
     *
     * @param  list   list
     * @param  index  index
     * @return true if the index is valid
     */
    public static boolean isValidIndex(List<?> list, int index) {
        if (list == null) {
            throw new NullPointerException("list == null");
        }

        return (index >= 0) && (index < list.size());
    }
}

Related Tutorials