From 51adbd3b20624158792da27fdf2479e05f22c7a2 Mon Sep 17 00:00:00 2001 From: shaper Date: Mon, 19 Aug 2002 23:22:09 +0000 Subject: [PATCH] Added getMaxValue(). Really fixed getMaxIndexes() to not freak out if values is null. git-svn-id: https://samskivert.googlecode.com/svn/trunk@813 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ArrayUtil.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java b/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java index 34419f0f..dd609ac0 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.13 2002/08/15 23:00:30 shaper Exp $ +// $Id: ArrayUtil.java,v 1.14 2002/08/19 23:22:09 shaper Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Walter Korman @@ -31,14 +31,33 @@ import com.samskivert.Log; */ public class ArrayUtil { + /** + * Returns the maximum value in the given array of values, or {@link + * Integer#MIN_VALUE} if the array is null or zero-length. + */ + public static int getMaxValue (int[] values) + { + int max = Integer.MIN_VALUE; + int vcount = (values == null) ? 0 : values.length; + for (int ii = 0; ii < vcount; ii++) { + if (values[ii] > max) { + // new max + max = values[ii]; + } + } + return max; + } + /** * Returns an array of the indexes in the given array of values that - * have the maximum value in the array. + * have the maximum value in the array, or a zero-length array if the + * supplied array of values is null or zero-length. */ public static int[] getMaxIndexes (int[] values) { int max = Integer.MIN_VALUE; - int num = 0, vcount = values.length; + int num = 0; + int vcount = (values == null) ? 0 : values.length; for (int ii=0; ii < vcount; ii++) { int value = values[ii];