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 is reverse function?
how can you catch multiple exceptions in java?
What is java console application?
Whats the difference between notify() and notifyall()?
What is the size of a string in java?
What are data types in oop?
Why is java logo a cup of coffee?
Does java initialize arrays to zero?
What is ‘is-a ‘ relationship in java?
What is the difference between iterator and enumeration ?
Is linkedlist thread safe in java?
Is the empty set a singleton?
How do you break a loop?
Define the term string pool?
What is overloading and overriding in java?