473,788 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 24338
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(ArrayLi st myList)
{
int max = Int32.MinValue;
for (i = 0; i<myList.Count ; i++)
{
int val = (int)myArrayLis t[i];
if (val > max)
max = val;
}
return max;
}
--
Hans Kesting
Nov 17 '05 #2

"Ada" <Ad*@discussion s.microsoft.com > wrote in message
news:32******** *************** ***********@mic rosoft.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(Arra yList 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*@discussion s.microsoft.com > wrote in message
news:32******** *************** ***********@mic rosoft.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*@discussion s.microsoft.com > wrote in message
news:DF******** *************** ***********@mic rosoft.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
13484
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 getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the second pass we can find the highest number which is less than the number we obtained in the 1st pass.
3
2202
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 increment it by one before adding data to the dataset. Thanks, Grant
3
2000
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 to retrieve one row: DataRowView rowViewAudit = (DataRowView) ((CurrencyManager)BindingContext).Current; Now I want to modify this line so it retrieves all the highlighted rows. Is it possible?
1
2410
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 gentle, I know I'm not a C wizard already, that's why I'm here. The program will generate a guess based on input from another program I've already written. It won't always guess the right answer, but it will guess right more often than any other...
2
9983
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 aynchronously so then in my code I have this to retrieve the ArrayList object. public void CalcCallback(IAsyncResult ar) { ArrayList arrResults = ws.MyFunction(ar); }
7
3357
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
8811
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
5563
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 1.5.. i have created a form bean to get the data from the user. then in my action class i m trying to get a database connection and adding the values of the user. and i also wanna show them back to him. its a dummy project for my learning(to get...
13
2778
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" the arraylist pofile2.aspx.cs ArrayList list2 = new ArrayList(); //query to get uid
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.