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
Rep Power: 0
mew is on a distinguished road
Default 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
  #2 (permalink)  
Old 12-03-2007, 02:08 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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
Rep Power: 0
ravian is on a distinguished road
Default
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
Rep Power: 0
mew is on a distinguished road
Default
Thank you all of you.
It work fine.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-03-2007, 01:47 PM
Senior Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 102
Rep Power: 0
wsaryada is on a distinguished road
Default
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: Learn Java Programming by Examples - Blog: Java Programming Blog

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
Rep Power: 0
mew is on a distinguished road
Default
Thanks wsaryada. But I think the way you mentioned will be a little slower than Arrays.sort.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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 +2. The time now is 03:18 PM.



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