write java code to print second max number in the array

Answer Posted / sujeev

Ranjaram is correct.I modified and testes it.
public class test123 {
public static void main(String[] args){
int maxNumber = 0;
int secondMaxNumber = 0;

int[] anArray;
anArray =new int [10];
anArray[0] = 100;
anArray[1] = 200;
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;

if(anArray.length == 0){
System.err.println("Number array is empty");
return;
}
for(int i=0; i < anArray.length; i++){
int currNumber = anArray[i];
if(maxNumber < currNumber){
secondMaxNumber = maxNumber;
maxNumber = currNumber;
}else if(secondMaxNumber < currNumber){
secondMaxNumber = currNumber;
}
}
System.err.println("Max. number is "+maxNumber);
System.err.println("Second Max.
is "+secondMaxNumber);
}
}

Is This Answer Correct ?    2 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the list interface?

614


What data structures are used to perform recursion?

608


What is sorting in java?

543


What is difference between static and abstract class?

524


What does n mean?

523






Is string thread safe in java?

587


How does the garbage collector works in java?

575


Which one of the following suits the description of a string better: derived or primitive?

518


What is the difference between an if statement and a switch statement?

660


How will you compute size of a structure?

594


Why is stringbuffer faster than string?

497


Does A Class Inherit The Constructors Of Its Superclass?

541


What is the right data type to represent a price in java?

574


What is static import?

605


Is string is a class in java?

527