473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 24286
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
3
by: Grant | last post by:
Hello, How would I get the highest number from a column? The table has an ID column but it does not auto imcrement and I have to do it manually so I need to get the hihest integer value and...
3
by: VMI | last post by:
I'm using Binding Manager to retrieve the current table row that the user clicked on (one highlighted row). But if I want to retrieve all the highlighted rows, how can I do it? This is the code...
1
by: Sluggoman | last post by:
Hi all, Am floundering through a course in which C was not a pre-req, but the assignment is in C - If someone could point out where I am going way off the rails, I'd apprecciate it. Please be...
2
by: SteveR | last post by:
I want to return an ArrayList from my web service. I can write this part and everything compiles but I can't get the web application that uses the web service to compile. I call the service...
7
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have an int array and was just wondering if there is a way to get the highest value in the array? for instance, int myValues = new int { 0, 1, 2 } highest value is 2. thanks,
9
by: gavy7210 | last post by:
hello friends i am new to struts. i want to add the user information in the database and then display it back to him. i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk...
13
by: looper | last post by:
Hi, i am having a problem as im new to c#. I did an arraylist and store my "uid" and want to put it into a row of pictures pic 1 , pic 2 , pic 3, pic 4 this is my code for putting the "uid"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.