Tuesday, September 13, 2011

max() function and min() function to find out maximum and minimum number from an ArrayList


//Print the list value and also find out the maximum and minimum number from the list.
//MaxMinOfArrayLisExample.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class MaxMinOfArrayLisExample
{
public static void main(String arg[])
{
List li = new ArrayList();
li.add(23);
li.add(25);
li.add(27);
li.add(22);
li.add(29);
li.add(29);

Iterator it;
it = li.iterator();
System.out.println("The list is as follows");
while (it.hasNext())
{
System.out.println(it.next());
}

System.out.print("Max number from the list is ");
int maximum= Collections.max(li);
System.out.println(maximum);

System.out.print("Min number from the list is ");
int minimum= Collections.min(li);
System.out.println(minimum);
}
}


Output =>
The list is as follows
23
25
27
22
29
29
Max number from the list is 29
Min number from the list is 22

No comments:

Post a Comment