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
What is widening and narrowing in java? Discuss with an example.
What are the two types of exceptions in java? Which are the differences between them?
Why are functions called methods in java?
Can You Have Virtual Functions In Java?
How to sort an array in java without using sort method?
Which class is the superclass for all the classes?
What is the common usage of serialization? What exceptions occur during serialization?
Is hashset ordered java?
Does the order of public and static declaration matter in main method?
Explain features of interfaces in java?
Can memory leak in java?
What is the structure of java?
Explain parallel processing in java8?
What is the difference between a constructor and a method?
What are parsing rules?