Web Article: Generic Method Example in Java

public static  < T > void sum(List  sumList)
{
 Number sum = 0;
 for (Number n : sumList)
 {
     sum = sum.doubleValue() + n.doubleValue();
 }
 System.out.println(sum);
}
public static void main(String[] args)
{
 List < Integer > integerList = new ArrayList < >();
 integerList.add(1);
 integerList.add(2);
 integerList.add(3);
 sum(integerList);

 List < Float > floatList = new ArrayList < >();
 floatList.add(1.2f);
 floatList.add(2.2f);
 floatList.add(3.4f);
 sum(floatList);
}



Source: javacodegeeks

0 Respostas em "Web Article: Generic Method Example in Java"

Powered by Blogger