max value in array

Hello,can I use Collections.max() method to find a max value in array?Thanks,Alex
[109 byte] By [alexustasa] at [2007-11-26 15:47:48]
# 1
> can I use Collections.max() method to find a max> value in array?What does the documentation say?
kajbja at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...
# 2
Note that the Arrays class has a method named asList, that method returns a list view of an array.Kaj
kajbja at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...
# 3
It says thet arrays are the members of java collections framework but i cannot apply Collections.max() method to array...
alexustasa at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...
# 4
> i cannot apply Collections.max() method to array...That answers your original question: you cannotuse Collections.max() directly.But kajbj pointed out that you can turn an array into a Collectionby using Arrays.asList()
Suntra_Vineeta at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...
# 5

> It says thet arrays are the members of java

> collections framework but

The class Arrays is something very different from an array.

> i cannot apply Collections.max() method to array...

That's because Collections.max takes a collection as argument, and an array is not a collection.

Kaj

kajbja at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...
# 6
Thank you for yours replies,but anyway,could you suggest please easiest way to find max value in array?
alexustas_a at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...
# 7

If it is indeed an array, I would suggest writing

a simple loop that go through every element exactly once

(and along the way, you have a variable "max"

that records the maximum entry)

To use Collections.max() you would have to convert

the existing array into a Collection object, and then walk over

it... that introduces overhead.

Suntra_Vineeta at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...
# 8

> but anyway,could you suggest please easiest way to

> find max value in array?

If want to avoid writing your own algorithm and want a quick shortcut, try java.util.Arrays.sort() function and pick the last (or first) element in the resulting array.

Note: If you have object elements in the array, ensure that you either implement Comaprable interface on those elements or provide a Comparator implementation to the Array api.

-BJ

edited by: Bimalesh

Bimalesha at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...
# 9

> If want to avoid writing your own algorithm and want

> a quick shortcut, try java.util.Arrays.sort()

> function and pick the last (or first) element in the

> resulting array.

I'd hardly call it an algorithm. It's just one for statement to loop through the array once to find the max value. This approach will find it in O(n), where the sort approach will take an average of O(n*log(n)).

So I'd say the quickest way will be to loop through the array just once.

prometheuzza at 2007-7-8 22:07:14 > top of Java-index,Java Essentials,New To Java...