views:

2218

answers:

4

It's trivial to write a function to determine the min/max value in an array, such as:

/**
 * 
 * @param chars
 * @return the max value in the array of chars
 */
private static int maxValue(char[] chars) {
 int max = chars[0];
 for (int ktr = 0; ktr < chars.length; ktr++) {
  if (chars[ktr] > max) {
   max = chars[ktr];
  }
 }
 return max;
}

but isn't this already done somewhere?

+4  A: 

Yes, it's done in the Collections class. Note that you will need to convert your primitive char array to a Character[] manually.

A short demo:

import java.util.*;

public class Main {

    public static Character[] convert(char[] chars) {
        Character[] copy = new Character[chars.length];
        for(int i = 0; i < copy.length; i++) {
            copy[i] = Character.valueOf(chars[i]);
        }
        return copy;
    }

    public static void main(String[] args) {
        char[] a = {'3', '5', '1', '4', '2'};
        Character[] b = convert(a);
        System.out.println(Collections.max(Arrays.asList(b)));
    }
}
Bart Kiers
can you show me the code to do that?
Rosarch
Collections.min(myCollection); If you want to use it for arrays, you can do it like Collections.min(Arrays.asList(myArray));
Zed
converting a `char []` to a `Character []` only to determine the maximum is quite inefficient - better create a utility class with static methods for each primitive type similar to `java.util.Arrays`: http://java.sun.com/javase/6/docs/api/java/util/Arrays.html
Christoph
@Christoph: yes, if the size of the array is large, I would agree. Simply stating it is "inefficient" does not make sense if the application in question makes many database calls and/or I/O operations and the size of the array is (relative) small.
Bart Kiers
you should use `Character.valueOf(chars[i])` instead of `new Character(chars[i])` for performance reasons: http://java.sun.com/javase/6/docs/api/java/lang/Character.html#valueOf%28char%29
Christoph
@Christoph: edited.
Bart Kiers
+1  A: 

Here's a utility class providing min/max methods for primitive types: Primitives.java

Christoph
+2  A: 

Using Commons Lang (to convert) + Collections (to min/max)

import java.util.Arrays;
import java.util.Collections;

import org.apache.commons.lang.ArrayUtils;

public class MinMaxValue {

    public static void main(String[] args) {
        char[] a = {'3', '5', '1', '4', '2'};

        List b = Arrays.asList(ArrayUtils.toObject(a));

        System.out.println(Collections.min(b));
        System.out.println(Collections.max(b));
   }
}
Michael Rutherfurd
+1  A: 

The Google Guava library has min and max methods in its Chars, Ints, Longs, etc. classes.

So you can simply use:

Chars.min(myarray)

No conversions are required and presumably it's efficiently implemented.

Andrew McKinlay
It's implemented more or less like in the question except it throws an IllegalArgumentException for an array of length 0. (http://code.google.com/p/guava-libraries/source/browse/trunk/src/com/google/common/primitives/Chars.java)
ColinD