0

I know how to find minimum and maximum in an array. If a method lets say was called fMax():

public static double fMax(Object[] stuff)

The parameter is an array object how would I go about finding the max of this array? I cannot just do. Okay so how would I do this if I want the method to return a double and if the memory hasnt been allocated for the parameter named stuff then it will return the value NEGATIVE_INFINITY in the Double class, otherwise the return value will be the maximum value from the elements in the stuff array

Object max = stuff[0];
for (int i = 0; i < stuff.length; i++) {
    if (data[i] > max) {
        max = stuff[i];
    }
}
9
  • 5
    Object isn't comparable. Do you have some object that at least implements Comparable? (Also, are you really attached to the array? Want to upgrade to a List?) Commented Jan 21, 2016 at 5:23
  • okay I see. if object isnt comparable then how would you go about finding the maximum value Commented Jan 21, 2016 at 5:33
  • 4
    What makes an Object greater than another Object? Commented Jan 21, 2016 at 5:37
  • 1
    If the object isn't comparable, you can't compare them, therefore "maximum" and "minimum" have no meaning. Commented Jan 21, 2016 at 5:41
  • 1
    If you have a Double[], then you can just do it like you're doing it now. Commented Jan 21, 2016 at 5:41

3 Answers 3

1

To find the maximum of something, either a) that something needs to implement the Comparable interface b) you need to have some sort of explicit criteria for determining what maximum is, so you can put that in an instance of Comparator

Object itself isn't going to have anything useful for sorting. If you subclass object, you could sort based on the components of that object.

public class Example implements Comparable
{
    int sortableValue = 0;

    public Example (int value)
    {
        this.sortableValue = value;
    }

    public int compareTo(Example other)
    {
        return Integer.compare(this.sortableValue, other.sortableValue);
    }
}

That's an object definition that has a natural sorting order. Java can look at that with any of the built in sorting algorithms and know the order they belong in.

If you don't provide java with a means of determining how an object has greater or lesser relative value compared to another object of the same type, it won't figure it out on its own.

Sign up to request clarification or add additional context in comments.

Comments

0

Object is not comparable, you need a definite type if you want to compare values, sort or find something. Streams are the most powerful, versatile tools for the job, this here will solve your problem if your want to find min/max of an array of Double :

Double[] arr = {1d, 2d, 3d, 4d};
Double min = Arrays.asList(arr).stream().parallel().min(Double::compare).get();
Double max = Arrays.asList(arr).stream().parallel().max(Double::compare).get();    

Comments

0
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);

Now, just compare the new primitive array that we made from the object. If you don't need the object after this, and you aren't planning on returning an array object, then make your original array null, to take up less memory.

Check this:

How to compare two object arrays in Java?

1 Comment

Yes - and now you're contradicting yourself with your own words.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.