write java code to print second max number in the array

Answer Posted / ibrahim

import java.util.ArrayList;
public class maxSec {

public static void main(String[] args) {
int maxFi=0;
int maxSe=0;
ArrayList<Integer> arl=new ArrayList<Integer>();

arl.add(4); arl.add(8); arl.add(2);
arl.add(6); arl.add(7); arl.add(8);

for(int i=0; i<arl.size(); i++){
if (maxFi< arl.get(i))
maxFi= arl.get(i);
}

for(int i=0; i<arl.size(); i++){
if (maxSe <arl.get(i)){
if (arl.get(i)<maxFi ){

maxSe=arl.get(i);
}
}
}
System.out.println(maxFi);
System.out.println(maxSe);

}

}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Are there structures in java?

559


How to make a write-only class in java?

579


What is the difference between numeric and integer?

514


How do you differentiate abstract class from interface?

532


What is the indent key?

592






How do you check if a string contains only numeric digits?

620


What do you understand by java?

566


What about static nested classes in java?

592


What are the uses of synchronized keyword?

558


Explain some best practices you would apply while using collection in java?

572


What is difference between arraylist and list in java?

585


Is nullpointerexception checked or unchecked?

559


What is the use of flag?

583


Explain the inheritance?

577


How garbage collection is done in java?

551