Results 1 to 6 of 6
  1. #1
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    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 :confused:
    Java Code:
    int []array = new int[10];
    
    for(int i=0;i<10;i++)
    	array[i] = i;

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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]);

  3. #3
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Yes, Arrays.sort can serve the purpose. Try it.

  4. #4
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default

    Thank you all of you.
    It work fine.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    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:

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

    Java Code:
    Collections.min(Arrays.asList(n));
    Last edited by wsaryada; 12-03-2007 at 12:52 PM.

  6. #6
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default

    Thanks wsaryada. But I think the way you mentioned will be a little slower than Arrays.sort.

Similar Threads

  1. How to use Idref Element and its advantages
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-30-2008, 10:04 AM
  2. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 08:44 AM
  3. How to use Idref Element and its advantages
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:37 PM
  4. a no such element exception
    By headlice1 in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:36 PM
  5. Replies: 4
    Last Post: 07-31-2007, 05:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •