Hi Guest

MobileUser

1 HomePage 1 Jobs 1 WalkIn 1Articles  

Cat Exam

 :) Latest Topic
general knowledge
VB.NET
SQL Query
UDDI
CLASS
JavaScript Questions
SharePoint Interview
SilverLight
WCF
beans
 ASP.NET
 :) Hot Jobs
 
 :) Latest Articles


  

 


Write code to find the Second Highest number in an ArrayList in Java?
Question Posted on 07 Apr 2021

Home >> Java >> Core Java >> Write code to find the Second Highest number in an ArrayList in Java?




Write code to find the Second Highest number in an ArrayList in Java?
Below is the code to get the Higest and the second higest number from given array here i have created a class for that.

public class SecondHigest
{
  public static void main(String[] args)
  {
   int array[] = { 1, 2, 3, 4, 5 ,6 ,7 ,8 ,9 ,10 ,11 ,12, 13, 14};
   int highNumber = 0;
   int secondHighNumber = 0;
   for (int i = 0; i < array.length; i++)
   {
    if (array[i] > highNumber)
    {
     secondHighNumber = highNumber;
     highNumber = array[i];
    }
    else if (array[i] > secondHighNumber)
    {
     secondHighNumber = array[i];
    }
   }
   System.out.println("\nSecondHighest Number is:" + secondHighNumber);
   System.out.println("Highest Number is: " +highNumber);
  }
}
Output:

SecondHighest Number is:13

Highest number is: 14
0
0



 
 Input Your Comments Or suggestions(If Any)
 Name
 Email(optional)
 Comments


Other Important Questions
what is an object
what is OOAD
difference between c++ and java
what is a transient variable in java
difference between C and Java on pointers






 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |