From 82bd4c7f1fae62c806159fe78c61efec2d23fcaa Mon Sep 17 00:00:00 2001 From: ray Date: Wed, 23 Jul 2008 23:39:45 +0000 Subject: [PATCH] More changes requested by the puzzle pirates. - Added ArrayUtil.indexOf(int[], int). This has never been added, in all our years, because it exists in IntListUtil. IntLists are sort of different from int[]s, they're designed such that a 0 is an empty slot. But indexOf() works the same in either one, so let's make ArrayUtil the definitive container of this code, and have IntListUtil use ArrayUtil. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2344 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/ArrayUtil.java | 16 ++++++++++++++++ src/java/com/samskivert/util/IntListUtil.java | 16 ++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/java/com/samskivert/util/ArrayUtil.java b/src/java/com/samskivert/util/ArrayUtil.java index a5419690..1307d0e0 100644 --- a/src/java/com/samskivert/util/ArrayUtil.java +++ b/src/java/com/samskivert/util/ArrayUtil.java @@ -48,6 +48,22 @@ public class ArrayUtil return -1; } + /** + * Looks for an element that is equal to the supplied value and returns its index in the array. + * + * @return the index of the first matching value if one was found, 1 otherwise. + */ + public static int indexOf (int[] values, int value) + { + int count = (values == null) ? 0 : values.length; + for (int ii = 0; ii < count; ii++) { + if (values[ii] == value) { + return ii; + } + } + return -1; + } + /** * Looks for an element that is equal to the supplied value and returns its index in the array. * diff --git a/src/java/com/samskivert/util/IntListUtil.java b/src/java/com/samskivert/util/IntListUtil.java index 48f14377..0c3ddd1b 100644 --- a/src/java/com/samskivert/util/IntListUtil.java +++ b/src/java/com/samskivert/util/IntListUtil.java @@ -175,13 +175,7 @@ public class IntListUtil */ public static boolean contains (int[] list, int value) { - int llength = list.length; // no optimizing bastards - for (int i = 0; i < llength; i++) { - if (list[i] == value) { - return true; - } - } - return false; + return (-1 != indexOf(list, value)); } /** @@ -195,13 +189,7 @@ public class IntListUtil */ public static int indexOf (int[] list, int value) { - int llength = list.length; // no optimizing bastards - for (int i = 0; i < llength; i++) { - if (list[i] == value) { - return i; - } - } - return -1; + return ArrayUtil.indexOf(list, value); } /**