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

How can you share data between two thread in Java?

712


What is indexof?

716


why Interface used?

716


What is finally block?

741


explain different ways of using thread? : Java thread

751


Does java arraylist maintain insertion order?

731


Give a practical example of singleton class usage?

769


What is var keyword ?

911


How many bytes is a url?

741


How big is a gigabyte?

807


What is a qualifier in a sentence?

735


Why we use multi threading instead of multiprocessing?

754


Is a method a function?

769


Is special character in java?

791


What are the important methods of java exception class?

729