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

What kind of variables a class can consist of?

636


Can private class be inherited in java?

551


What is boolean in java?

513


what is the swingutilities.invokelater(runnable) method for? : Java thread

549


Explain the hierarchy of java exception classes?

598






Why there are some null interface in JAVA? What does it mean? Give some null interface in JAVA?

617


What are the 6 boolean operators?

553


How do you include a string in java?

524


Does constructor return any value?

585


how can you catch multiple exceptions in java?

543


Why packages are used?

548


What is constructor in java ?

636


What is native method in java?

567


What is field name?

587


What does the “static” keyword mean? Can you override private or static method in java?

633