VMMF VMMF - 3 months ago 6
Java Question

Finding the maximum element and its position in an array of Generics and returning an array with both

I have repeated the same code in 4 different functions to find the greatest value in an array for 4 different data types which are : int, short, double and float. Except for the different data types the code is exactly the same so I decided to implement a single function that coud work with generic data types. I managed to do this as long as I only return the maximum element, but my problem is that I also want to return the index of the array at which the maximum element was found. My defective code is:

public static <T extends Comparable <? super T> > T[] maxOfArray (final T [] array)
{
T maximum = array[0];
Integer index = 0;

// Unchecked cast warning, the only thing I could find to create a T array
T[] maxAndIndex = (T[])new Object[2];

for (int i = 1; i < array.length; i++)
{
if (maximum.compareTo(array[i]) < 0)
{
maximum = array[i];
index = i;
}
}

maxAndIndex[0] = maximum;
maxAndIndex[1] = index; // Unable to cast problem

return maxAndIndex;

}


And my questions are:


  1. Is it a good idea from the performance point of view to create a generic function instead of 4 different functions each working with its own data type. I'm asking this because Math.java doesn't implement a generic. Is it for performace reasons?

  2. Is it a bad idea to return a T array? Perhaps as I'm only working with numerics I could always return an array of 2 doubles

  3. How do I store the index in the generic array or how do I store the maximum generic value in a double array?



Please notice that I do not whish to create another class or return the index as an input parameter unless there isn't ANY OTHER option, I would like to be able to use the function in this way (with an array as input and an array as output), because it is the way in which the other 4 were created

Answer
  1. Is it a good idea from the performance point of view to create a generic function instead of 4 different functions each working with its own data type. I'm asking this because Math.java doesn't implement a generic. Is it for performace reasons?

There are no array methods at all in java.lang.Math. That's more likely the reason that there is no method for generic arrays.

I don't think there would be a performance reason not to use generic methods.

  1. Is it a bad idea to return a T array? Perhaps as I'm only working with numerics I could always return an array of 2 doubles

It is a bad idea. You lose the semantics of the result that:

  1. there are two values;
  2. the first value is the maximum;
  3. the second value is the index.

And this doesn't mention the fact that you've had to do some jiggery pokery to create the array.

  1. How do I store the index in the generic array or how do I store the maximum generic value in a double array?

You can't. As @ElliotFrisch points out, that you can't store an int and a T in the array in general (e.g. what if T is String?). But hopefully the points I mention above hint that you don't really want to.

In fact, the only type which you can return in general is Object[], since that is the common supertype of the Integer index and T value. And this renders the use of generic unnecessary: arrays are covariant, so you can simply make the parameter Object[] too.

This adds a fourth point to the list above:

  1. You lose all compile-time type information.

If you were to return the index of the maximal element, you can trivially get the value of the maximal element:

int maxIndex = maxOfArray(arr);
T value = arr[maxIndex];

If you really think that you actually need to return both index and value, create a little result class, and return an instance of this:

class Result<T> {
  final int index;
  final T value;

  Result(int index, T value) { /* initialize fields */ }

  // Getters, if you want.
}

This preserves the semantics that you lose by trying to return an array.

Comments