write java code to print second max number in the array
Answer Posted / himesh mistry
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class SecondMax {
/**
* @param args
*/
public static void main(String[] args) {
int[] numbers = {9,4,8,0,0,5,9,1,4,2};
Set arrSet = new HashSet();
for (int i=0;i<numbers.length;i++) {
arrSet.add(numbers[i]);
}
ArrayList s = new ArrayList(arrSet);
Collections.sort(s);
System.out.println("Second element : " +
s.get(s.size()-2));
}
}
Is This Answer Correct ? | 16 Yes | 5 No |
Post New Answer View All Answers
What kind of variables a class can consist of?
Can private class be inherited in java?
What is boolean in java?
what is the swingutilities.invokelater(runnable) method for? : Java thread
Explain the hierarchy of java exception classes?
Why there are some null interface in JAVA? What does it mean? Give some null interface in JAVA?
What are the 6 boolean operators?
How do you include a string in java?
Does constructor return any value?
how can you catch multiple exceptions in java?
Why packages are used?
What is constructor in java ?
What is native method in java?
What is field name?
What does the “static” keyword mean? Can you override private or static method in java?