package com;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class Test3 {
/**
* 计算链表、数组列表或数组中最大元素
* @param c
* @return
*/
public static T max(Collection c) {
if (c.isEmpty())
throw new NoSuchElementException();
Iterator iter = c.iterator();
T largest = iter.next();
while (iter.hasNext()) {
T next = iter.next();
if (largest.compareTo(next) < 0)
largest = next;
}
return largest;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(111);
list.add(8787878);
System.out.println(max(list));
List lista = new ArrayList();
lista.add("555");
lista.add("2");
lista.add("3");
lista.add("111");
lista.add("999");
System.out.println(max(lista));
}
}