Example usage for java.lang IndexOutOfBoundsException IndexOutOfBoundsException

List of usage examples for java.lang IndexOutOfBoundsException IndexOutOfBoundsException

Introduction

In this page you can find the example usage for java.lang IndexOutOfBoundsException IndexOutOfBoundsException.

Prototype

public IndexOutOfBoundsException(int index) 

Source Link

Document

Constructs a new IndexOutOfBoundsException class with an argument indicating the illegal index.

Usage

From source file:Main.java

public static int getBit(int[] bits, int index) throws IndexOutOfBoundsException {
    if (index < 0 || index > bits.length * 32)
        throw new IndexOutOfBoundsException("index = " + index);
    return (bits[index >> 5] >> (index & 0x1F)) & 1;
}

From source file:Main.java

public static int setBit(int[] bits, int index) throws IndexOutOfBoundsException {
    if (index < 0 || index > bits.length * 32)
        throw new IndexOutOfBoundsException("index = " + index);
    return bits[index >> 5] |= 1 << (index & 0x1F);
}

From source file:Main.java

public static int resetBit(int[] bits, int index) throws IndexOutOfBoundsException {
    if (index < 0 || index > bits.length * 32)
        throw new IndexOutOfBoundsException("index = " + index);
    return bits[index >> 5] &= ~(1 << (index & 0x1F));
}

From source file:Main.java

public static ImageView.ScaleType getScaleTypeByOrdinal(int ordinal) {
    if (ordinal < 0 || ordinal >= ImageView.ScaleType.values().length) {
        throw new IndexOutOfBoundsException("Invalid Ordinal");
    }/*from  w  w  w  .j a  va2s .c  om*/
    return ImageView.ScaleType.values()[ordinal];
}

From source file:Main.java

public static <T> T get(List<T> list, int index) {
    if (index < 0) {
        throw new IndexOutOfBoundsException("Index cannot be negative: " + index);
    }//from ww w. j av  a2s  .  co m
    if (list == null || index > list.size() - 1) {
        return null;
    }
    return list.get(index);
}

From source file:Main.java

private static void checkOffset(String paramString, int paramInt, char paramChar)
        throws IndexOutOfBoundsException {
    char c = paramString.charAt(paramInt);
    if (c != paramChar)
        throw new IndexOutOfBoundsException("Expected '" + paramChar + "' character but found '" + c + "'");
}

From source file:Main.java

public static String nthElement(final String s, final String separator, final int index) {
    final String[] list = s.split(separator);
    if (list.length <= 0)
        return null;
    if (index >= list.length)
        throw new IndexOutOfBoundsException("Could not get element " + index + " in " + s);
    return list[list.length - 1];
}

From source file:Main.java

public static List<String> merge(final List<String> list, final int index) {
    if (list.isEmpty()) {
        throw new IndexOutOfBoundsException("Cannot merge empty list");
    } else if (index + 1 >= list.size()) {
        throw new IndexOutOfBoundsException("Cannot merge last element");
    } else {// www  .j a v a 2  s  .co  m
        final List<String> result = new ArrayList<String>(list);
        result.set(index, list.get(index) + list.get(index + 1));
        result.remove(index + 1);
        return result;
    }
}

From source file:Main.java

public static <T> T getFirst(List<T> collection) {
    checkIsNull(collection);/*from  w w  w .  j  a v a2s  . co  m*/

    if (collection.size() < 1) {
        throw new IndexOutOfBoundsException("The supplied collection is null or empty.");
    }

    return collection.get(0);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] delete(T[] array, int index) {
    int length = array.length;
    if (index < 0 || index >= length) {
        throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
    }//  www .  j a  v a2 s  .  c  o m

    T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), length - 1);
    System.arraycopy(array, 0, result, 0, index);
    if (index < length - 1) {
        System.arraycopy(array, index + 1, result, index, length - index - 1);
    }

    return result;
}