Need to get the min&max from array

Hi Can anyone tell me the methods (if any) for getting out the min and max numbers from an array of integers?Thanks
[136 byte] By [getusamaa] at [2007-11-26 15:33:15]
# 1
Write a loop and keep track of the min and max as you goorSort the array, then get the first and last item.I'm sure there are other ways.
camickra at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
okay ... i thout there might be a methodthanks
getusamaa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 3
java.util.Collections has min() and max() methods, but then you'd have to put your data into a collection.
DrLaszloJamfa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 4
thats wonderful ..... that should work for me .... i can convert my entries to the collection to an integer when usingThanks a lot
getusamaa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 5
> java.util.Collections has min() and max() methods,> but then you'd have to put your data into a> collection.i'm sorry but there are no methods as such in this class
getusamaa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 6
Huh? Here is the API for [url= http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#max(java.util.Collection)]max[/url]. Min is just above it on the page.
DrLaszloJamfa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 7

oops ... i'm so sorry i was looking at the Collection Interface ....

thanks you so much ...

I hope it works as the same way as vectors since this is the first time i'll be using it

just two more quick questions

1) can i declare its object as two-dimentinal (like the way you can do to a vector)?

2) if i store all integer types in the collection object, it sould not have any problems getting the min and max, right?

Thanks a lot

getusamaa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 8

> I hope it works as the same way as vectors since this is the first time i'll be using it

What are you refering to here? Collections? No, Collections is a utility class, consisting of handy static methods you call directly.

> 1) can i declare its object as two-dimentinal (like the way you can do to a vector)?

I don't understand what you are referring to here. Also, java.util.Vector is one-diemensional, although you can nest it: Vector<Vector><Integer>>.

> 2) if i store all integer types in the collection object, it sould not have any problems getting the min and max, right?

Maybe a demo is in order (I'll do it without generics).

List list = new ArrayList();

list.add(new Integer(5));

list.add(new Integer(-1));

list.add(new Integer(17));

list.add(new Integer(5));

Integer a = (Integer) Collections.max(list);

Integer b = (Integer) Collections.min(list);

DrLaszloJamfa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 9
Thank you ... its most appreciatedBy the way, I have used multi-dimensional vector like:Vector v[] = new Vector[3];Cheers
getusamaa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 10

> By the way, I have used multi-dimensional vector

> like:

>

> Vector v[] = new Vector[3];

That's an array of three vectors. Why insist on calling it "multi-dimensional".

One more thing: most forum members will tell you to move up from the historical Vector class to ArrayList or some other List type.

DrLaszloJamfa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 11
oops ... my mistake Does the below code make it multi-Dimentional?Vector v[][] = new Vector[3][6];yea, i've been hearing about the use of other List types. Thanks for remindingRegards
getusamaa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...
# 12

Vector[][] v = new Vector[3][6];

Yes, informally, that would be called "multi-dimensional", but really it's an array of arrays of Vectors. You have to compare that to languages like VB that really have multi-dimensional arrays. One nice thing about v being an array of arrays is that you can refer to individual rows:

Vector[] rowj = v[j];

DrLaszloJamfa at 2007-7-8 21:50:22 > top of Java-index,Desktop,Core GUI APIs...