469,289 Members | 2,156 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

retrieve highest value in ArrayList

Ada
hello folks,

is there a way to retrieve the highest value in the ArrayList?
let say i have a value in the array: {1, 4, 15, 3, 7}
it should return a value 15 as the result.

i've looked at the Sort Method ( ) but doesn't seem like it can do the job
or am i missing something?

Nov 17 '05 #1
6 23507
Ada wrote:
hello folks,

is there a way to retrieve the highest value in the ArrayList?
let say i have a value in the array: {1, 4, 15, 3, 7}
it should return a value 15 as the result.

i've looked at the Sort Method ( ) but doesn't seem like it can do the job
or am i missing something?


A Sort method should return a sorted list, so take the first or last
value (depending on sort order) to get the "highest" value.

But I think it would be quicker just to loop (once!) through the list:

public int FindMax(ArrayList myList)
{
int max = Int32.MinValue;
for (i = 0; i<myList.Count; i++)
{
int val = (int)myArrayList[i];
if (val > max)
max = val;
}
return max;
}
--
Hans Kesting
Nov 17 '05 #2

"Ada" <Ad*@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
hello folks,

is there a way to retrieve the highest value in the ArrayList?
let say i have a value in the array: {1, 4, 15, 3, 7}
it should return a value 15 as the result.

i've looked at the Sort Method ( ) but doesn't seem like it can do the job
or am i missing something?

If you can assume all elements are the same type, int32 for example, you can
write a simple method to return the largest value, something like this
(thrown together with no regard for anything but basic functionality)...

public static int getBiggest(ArrayList bigList)

{

int large = Convert.ToInt32(bigList[0]);

for(int i = 1; i < bigList.Count; i++)

{

int c = Convert.ToInt32(bigList[i]);

if (c > large)

{

large = c;

}

}

return large;

}

Or, you could just call the Sort() method, then pull the last element.


Nov 17 '05 #3
hi,

If the array is not sorted you have no other option but to iterate on it.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ada" <Ad*@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
hello folks,

is there a way to retrieve the highest value in the ArrayList?
let say i have a value in the array: {1, 4, 15, 3, 7}
it should return a value 15 as the result.

i've looked at the Sort Method ( ) but doesn't seem like it can do the job
or am i missing something?

Nov 17 '05 #4
Ada
thanks guys for the response.

now, the next question.
iterate thru the ARRAY LIST or use the SORT METHOD and pull out the last
element (assuming sorting in ascending order), is there a tool in VS.NET or
3rd party to measure the execution speed or/and efficiency of the code?

Nov 17 '05 #5
Ada wrote:
thanks guys for the response.

now, the next question.
iterate thru the ARRAY LIST or use the SORT METHOD and pull out the last
element (assuming sorting in ascending order), is there a tool in VS.NET or
3rd party to measure the execution speed or/and efficiency of the code?


Iterating would require just one pass through the list.
Even the most efficient sorting method should require more.

You could test this with something like:
- remember DateTime.Now
- execute iteration or sort a lot of times (10.000 or more)
- get a new Datetime.Now
- print the difference between the dates

--
Hans Kesting
Nov 17 '05 #6
Hi,

IIRC
iterating takes n comparision , sorting require nlogn

so unless you need it sorted for some other reason, just iterate on it.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ada" <Ad*@discussions.microsoft.com> wrote in message
news:DF**********************************@microsof t.com...
thanks guys for the response.

now, the next question.
iterate thru the ARRAY LIST or use the SORT METHOD and pull out the last
element (assuming sorting in ascending order), is there a tool in VS.NET
or
3rd party to measure the execution speed or/and efficiency of the code?

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Jaspreet | last post: by
3 posts views Thread by Grant | last post: by
1 post views Thread by Sluggoman | last post: by
3 posts views Thread by =?Utf-8?B?cm9kY2hhcg==?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.