Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { /** * Gets an entry of given list with given index, but safely.<br> * That means if the index is greater or lower than the allowed * boundaries this method will either use the first index or the last index.<br> * * @param eList The element's list * @param index The index to list the entry from * @param defaultVal If the value would be null * @param <E> The element's type * @return The element (or null if the list is empty) */ public static <E> E getEntrySafely(List<E> eList, int index, E defaultVal) { if (index < 0) index *= -1; if (eList.isEmpty()) return defaultVal; if (index >= eList.size()) index = eList.size() - 1; else if (index < 0) index = 0; if (index >= eList.size()) return defaultVal; return eList.get(index); } }