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


Please Help Members By Posting Answers For Below Questions

Which is better stringbuilder or stringbuffer?

700


How does list work in java?

689


What is consumer interface?

749


If an object is garbage collected, can it become reachable again?

762


What are java packages? What is the significance of packages?

753


Discuss different types of errors that generally occur while programming.

780


what is instanceof operator used in java?

774


What is scanner in java?

727


How do you download stubs from Remote place?

1527


Does the order of public and static declaration matter in main method?

819


What is the difference between the prefix and postfix forms of the ++ operator?

768


How do you represent a space in regex java?

717


What do you understand by the term string pool?

753


What is t type java?

802


What are the different access modifiers available in java?

777