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 do you understand by final value?
Write a program to find maximum and minimum number in array?
What is an object's lock and which object's have locks in java programming?
Does collectionutils isempty check for null?
Why deletion in linkedlist is fast than arraylist?
How do you reverse sort a list in java?
Is there a way to increase the size of an array after its declaration?
What is string pool in java?
What is OOP's Terms with explanation?
What is the difference between declaration and definition in java?
What is the difference between Java1.4 and Java1.5
What does @override mean?
How to make a read-only class in java?
How many types of memory areas are allocated by JVM in java?
What class allows you to read objects directly from a stream in java programming?