@Test public void testList2Array() { int a1[] = (int[]) ReflectHelper.list2Array(int[].class, asList(1, 2, 3)); assertArrayEquals(new int[] { 1, 2, 3 }, a1); @SuppressWarnings("unchecked") int a2[][] = (int[][]) ReflectHelper.list2Array(int[][].class, asList(asList(1, 2), asList(3, 4))); assertArrayEquals( new int[][] { new int[] { 1, 2 }, new int[] { 3, 4 } }, a2); }
public static Object list2Array(Class<?> targetType, Object src) { if (targetType.isArray() && src instanceof List) { List<?> list = (List<?>) src; int len = list.size(); Object dest = Array.newInstance(targetType.getComponentType(), len); for (int i = 0; i < len; ++i) { Array.set(dest, i, list2Array(targetType.getComponentType(), list.get(i))); } return dest; } else { return src; } }
ReflectHelper { public static Object list2Array(Class<?> targetType, Object src) { if (targetType.isArray() && src instanceof List) { List<?> list = (List<?>) src; int len = list.size(); Object dest = Array.newInstance(targetType.getComponentType(), len); for (int i = 0; i < len; ++i) { Array.set(dest, i, list2Array(targetType.getComponentType(), list.get(i))); } return dest; } else { return src; } } }
ReflectHelper { public static Object list2Array(Class<?> targetType, Object src) { if (targetType.isArray() && src instanceof List) { List<?> list = (List<?>) src; int len = list.size(); Object dest = Array.newInstance(targetType.getComponentType(), len); for (int i = 0; i < len; ++i) { Array.set(dest, i, list2Array(targetType.getComponentType(), list.get(i))); } return dest; } else { return src; } } private  ReflectHelper(); }
ReflectHelper { public static Object list2Array(Class<?> targetType, Object src) { if (targetType.isArray() && src instanceof List) { List<?> list = (List<?>) src; int len = list.size(); Object dest = Array.newInstance(targetType.getComponentType(), len); for (int i = 0; i < len; ++i) { Array.set(dest, i, list2Array(targetType.getComponentType(), list.get(i))); } return dest; } else { return src; } } private  ReflectHelper(); static Object list2Array(Class<?> targetType, Object src); }
ReflectHelper { public static Object list2Array(Class<?> targetType, Object src) { if (targetType.isArray() && src instanceof List) { List<?> list = (List<?>) src; int len = list.size(); Object dest = Array.newInstance(targetType.getComponentType(), len); for (int i = 0; i < len; ++i) { Array.set(dest, i, list2Array(targetType.getComponentType(), list.get(i))); } return dest; } else { return src; } } private  ReflectHelper(); static Object list2Array(Class<?> targetType, Object src);  }