GenericsUtil { public static <T> T[] toArray(Class<T> c, List<T> list) { @SuppressWarnings("unchecked") T[] ta= (T[])Array.newInstance(c, list.size()); for (int i= 0; i<list.size(); i++) ta[i]= list.get(i); return ta; }  static Class<T> getClass(T t); static T[] toArray(Class<T> c, List<T> list); static T[] toArray(List<T> list); static boolean isLog4jLogger(Class<?> clazz);  }
@Test public void testToArray() { List<Integer> list = new ArrayList<Integer>(); for(int i=0; i<10; i++) { list.add(i); } Integer[] arr = GenericsUtil.toArray(list); for (int i = 0; i < arr.length; i++) { assertEquals( "Array has identical elements as input list", list.get(i), arr[i]); } }
@Test public void testWithEmptyList() { try { List<String> list = new ArrayList<String>(); String[] arr = GenericsUtil.toArray(list); fail("Empty array should throw exception"); System.out.println(arr); }catch (IndexOutOfBoundsException ex) { } }
@Test public void testWithEmptyList2() { List<String> list = new ArrayList<String>(); String[] arr = GenericsUtil.<String>toArray(String.class, list); assertEquals("Assert list creation w/ no elements results in length 0", 0, arr.length); }