Using the @SafeVarargs Annotation : Diamond Operator « JDK 7 « Java






Using the @SafeVarargs Annotation


import java.util.ArrayList;

public class Test {

  public static void main(String[] args) {
    ArrayList<Integer> a1 = new ArrayList<>();
    a1.add(new Integer(1));
    a1.add(2);
    ArrayList<Float> a2 = new ArrayList<>();
    a2.add(new Float(3.0));
    a2.add(new Float(4.0));
    displayElements(a1, a2, 12);

  }

  @SafeVarargs
  public static <T> void displayElements(T... array) {
    for (T element : array) {
      System.out.println(element.getClass().getName() + ": " + element);
    }
  }

}

 








Related examples in the same category

1.Using the Diamond Operator for Constructor Type Inference
2.Using the Diamond Operator when type is not obvious
3.Using the Diamond Operator and Suppressing Unchecked Warnings(SuppressWarnings)