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 the list interface?
What data structures are used to perform recursion?
What is sorting in java?
What is difference between static and abstract class?
What does n mean?
Is string thread safe in java?
How does the garbage collector works in java?
Which one of the following suits the description of a string better: derived or primitive?
What is the difference between an if statement and a switch statement?
How will you compute size of a structure?
Why is stringbuffer faster than string?
Does A Class Inherit The Constructors Of Its Superclass?
What is the right data type to represent a price in java?
What is static import?
Is string is a class in java?