Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2007, 10:47 PM
mew mew is offline
Member
 
Join Date: Nov 2007
Posts: 70
mew is on a distinguished road
Max element in an Array
I have following array and I want to find the largest value in it. I know I can write a for loop and get the max value. But is there some read made method that Java provides for this very task
Code:
int []array = new int[10]; for(int i=0;i<10;i++) array[i] = i;
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-03-2007, 02:08 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
import java.util.Arrays; ... int[] n = { 3, 7, 4, 6, 5 }; int max = -Integer.MAX_VALUE; for(int j = 0; j < n.length; j++) { if(n[j] > max) max = n[j]; } System.out.println("loop max = " + max); System.out.println("original = " + Arrays.toString(n)); Arrays.sort(n); System.out.println("sorted n = " + Arrays.toString(n)); System.out.println("sorted max = " + n[n.length-1]);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-03-2007, 12:46 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Yes, Arrays.sort can serve the purpose. Try it.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-03-2007, 01:11 PM
mew mew is offline
Member
 
Join Date: Nov 2007
Posts: 70
mew is on a distinguished road
Thank you all of you.
It work fine.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-03-2007, 01:47 PM
Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 90
wsaryada is on a distinguished road
You may also use the Collections and Arrays class in-collaboration to get the maximum or the minimum value of an array. The code will be something like:

Code:
Collections.max(Arrays.asList(n));
On the other hand to get the minimum value of an array the code will be:

Code:
Collections.min(Arrays.asList(n));
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by wsaryada : 12-03-2007 at 01:52 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-03-2007, 06:26 PM
mew mew is offline
Member
 
Join Date: Nov 2007
Posts: 70
mew is on a distinguished road
Thanks wsaryada. But I think the way you mentioned will be a little slower than Arrays.sort.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Idref Element and its advantages Java Tip Java Tips 0 03-30-2008 11:04 AM
Unique element in an array revathi17 New To Java 2 12-31-2007 09:44 AM
How to use Idref Element and its advantages JavaBean Java Tips 0 09-26-2007 09:37 PM
a no such element exception headlice1 New To Java 1 08-07-2007 06:36 PM
selectSingleNode not returning element... schu777 XML 4 07-31-2007 06:19 PM


All times are GMT +3. The time now is 11:37 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org