How find second smallest and largest in number in array using java

0 votes
90 views
asked Jun 18, 2014 in JAVA by codeg (2,600 points)
Hi all

   How find second smallest and largest in number in array using java?

1 Answer

0 votes
answered Jun 18, 2014 by admin (8,115 points)

Hi Codeg..

   Try this piece of code..

  import java.util.*;

import java.io.*;
 
public class Function {
    int[] SecondGreatLow(int[] arr) {
        
        // code goes here
        /* Note: In Java the return type of a function and the
         parameter types being passed are defined, so this return
         call must match the return type of the function.
         You are free to modify the return type. */
        Arrays.sort(arr);
        return arr;
        
    }
    
    public static void main (String[] args) {
        // keep this function call here
        Scanner  s = new Scanner(System.in);
        Function c = new Function();
        int[] myIntArray = {1,2,3,84,66,29,75,87,93,87,6,26,57,};
        
        int[] x = c.SecondGreatLow(myIntArray);
        int length = x.length;
        
        System.out.println(x[1]);
        System.out.println(x[length-2]);
    }   
    
}
 
...