From 93386819d5b36b9bd46df12fde4187482d681d53 Mon Sep 17 00:00:00 2001 From: shaper Date: Tue, 3 Dec 2002 23:55:52 +0000 Subject: [PATCH] Added getMaxValueIndex(). git-svn-id: https://samskivert.googlecode.com/svn/trunk@957 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ArrayUtil.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java b/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java index 99ad3bbb..b4bd709c 100644 --- a/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java @@ -1,5 +1,5 @@ // -// $Id: ArrayUtil.java,v 1.19 2002/11/29 20:39:21 shaper Exp $ +// $Id: ArrayUtil.java,v 1.20 2002/12/03 23:55:52 shaper Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Walter Korman @@ -65,6 +65,28 @@ public class ArrayUtil return min; } + /** + * Returns the index of the maximum value in the given array of + * values, or -1 if the array is null or + * zero-length. + */ + public static int getMaxValueIndex (int[] values) + { + if (values == null || values.length == 0) { + return -1; + } + + int idx = 0; + int max = values[idx]; + for (int ii = 1; ii < values.length; ii++) { + if (values[ii] > max) { + max = values[ii]; + idx = ii; + } + } + return idx; + } + /** * Returns an array of the indexes in the given array of values that * have the maximum value in the array, or a zero-length array if the