throw an exception when calling random() on empty XxxList (from 221006)

This commit is contained in:
Ben Fry
2022-10-25 06:25:56 -04:00
parent a50d659779
commit 5e20b8a6fb
5 changed files with 27 additions and 0 deletions

View File

@@ -681,6 +681,9 @@ public class DoubleList implements Iterable<Double> {
* Return a random value from the list.
*/
public double random() {
if (count == 0) {
throw new ArrayIndexOutOfBoundsException("No entries in this DoubleList");
}
return data[(int) (Math.random() * count)];
}
@@ -690,6 +693,9 @@ public class DoubleList implements Iterable<Double> {
* randomSeed() from the specified sketch object.
*/
public double random(PApplet sketch) {
if (count == 0) {
throw new ArrayIndexOutOfBoundsException("No entries in this DoubleList");
}
return data[(int) sketch.random(count)];
}

View File

@@ -703,6 +703,9 @@ public class FloatList implements Iterable<Float> {
* Return a random value from the list.
*/
public float random() {
if (count == 0) {
throw new ArrayIndexOutOfBoundsException("No entries in this FloatList");
}
return data[(int) (Math.random() * count)];
}
@@ -712,6 +715,9 @@ public class FloatList implements Iterable<Float> {
* randomSeed() from the specified sketch object.
*/
public float random(PApplet sketch) {
if (count == 0) {
throw new ArrayIndexOutOfBoundsException("No entries in this FloatList");
}
return data[(int) sketch.random(count)];
}

View File

@@ -678,6 +678,9 @@ public class IntList implements Iterable<Integer> {
* Return a random value from the list.
*/
public int random() {
if (count == 0) {
throw new ArrayIndexOutOfBoundsException("No entries in this IntList");
}
return data[(int) (Math.random() * count)];
}
@@ -687,6 +690,9 @@ public class IntList implements Iterable<Integer> {
* randomSeed() from the specified sketch object.
*/
public int random(PApplet sketch) {
if (count == 0) {
throw new ArrayIndexOutOfBoundsException("No entries in this IntList");
}
return data[(int) sketch.random(count)];
}

View File

@@ -581,6 +581,9 @@ public class StringList implements Iterable<String> {
* Return a random value from the list.
*/
public String random() {
if (count == 0) {
throw new ArrayIndexOutOfBoundsException("No entries in this StringList");
}
return data[(int) (Math.random() * count)];
}
@@ -590,6 +593,9 @@ public class StringList implements Iterable<String> {
* randomSeed() from the specified sketch object.
*/
public String random(PApplet sketch) {
if (count == 0) {
throw new ArrayIndexOutOfBoundsException("No entries in this StringList");
}
return data[(int) sketch.random(count)];
}