diff --git a/core/src/processing/data/FloatList.java b/core/src/processing/data/FloatList.java index 700d25b3b..1d06b772a 100644 --- a/core/src/processing/data/FloatList.java +++ b/core/src/processing/data/FloatList.java @@ -423,12 +423,20 @@ public class FloatList implements Iterable { } + private String exceptionText(int count, int index, String method){ + return "The list size is "+count+". Trying to "+method+" the element at "+index+" which does not exist."; + } + /** * @webref floatlist:method * @brief Add to a value */ public void add(int index, float amount) { - data[index] += amount; + if (index < count) { + data[index] += amount; + } else { + throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "add")); + } } @@ -437,7 +445,11 @@ public class FloatList implements Iterable { * @brief Subtract from a value */ public void sub(int index, float amount) { - data[index] -= amount; + if (index < count) { + data[index] -= amount; + } else { + throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "sub")); + } } @@ -446,7 +458,11 @@ public class FloatList implements Iterable { * @brief Multiply a value */ public void mult(int index, float amount) { - data[index] *= amount; + if (index < count) { + data[index] *= amount; + } else { + throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "mult")); + } } @@ -455,7 +471,11 @@ public class FloatList implements Iterable { * @brief Divide a value */ public void div(int index, float amount) { - data[index] /= amount; + if (index < count) { + data[index] /= amount; + } else { + throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "div")); + } } diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index 091129b88..0cf5f207b 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -407,12 +407,20 @@ public class IntList implements Iterable { data[index]++; } + private String exceptionText(int count, int index, String method){ + return "The list size is "+count+". Trying to "+method+" the element at "+index+" which does not exist."; + } + /** * @webref intlist:method * @brief Add to a value */ public void add(int index, int amount) { - data[index] += amount; + if (index < count) { + data[index] += amount; + } else { + throw new IndexOutOfBoundsException(exceptionText(count, index, "add")); + } } /** @@ -420,7 +428,11 @@ public class IntList implements Iterable { * @brief Subtract from a value */ public void sub(int index, int amount) { - data[index] -= amount; + if (index < count) { + data[index] -= amount; + } else { + throw new IndexOutOfBoundsException(exceptionText(count, index, "sub")); + } } /** @@ -428,7 +440,11 @@ public class IntList implements Iterable { * @brief Multiply a value */ public void mult(int index, int amount) { - data[index] *= amount; + if (index < count) { + data[index] *= amount; + } else { + throw new IndexOutOfBoundsException(exceptionText(count, index, "mult")); + } } /** @@ -436,7 +452,11 @@ public class IntList implements Iterable { * @brief Divide a value */ public void div(int index, int amount) { - data[index] /= amount; + if (index < count) { + data[index] /= amount; + } else { + throw new IndexOutOfBoundsException(exceptionText(count, index, "div")); + } }