/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Restores the array of objects from the configuration. * * @param <K> the class of the item * @param conf the configuration to use * @param keyName the name of the key to use * @param itemClass the class of the item * @return restored object * @throws IOException : forwards Exceptions from the underlying * {@link Serialization} classes. */ public static <K> K[] loadArray(Configuration conf, String keyName, Class<K> itemClass) throws IOException { DefaultStringifier<K> stringifier = new DefaultStringifier<K>(conf, itemClass); try { String itemStr = conf.get(keyName); ArrayList<K> list = new ArrayList<K>(); String[] parts = itemStr.split(SEPARATOR); for (String part : parts) { if (!part.isEmpty()) list.add(stringifier.fromString(part)); } return GenericsUtil.toArray(itemClass, list); } finally { stringifier.close(); } }
T[] funcThatUsesToArray() { T[] arr = GenericsUtil.toArray(list); return arr; } }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
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); //use arr so that compiler will not complain }catch (IndexOutOfBoundsException ex) { //test case is successful } }
T[] funcThatUsesToArray() { T[] arr = GenericsUtil.toArray(list); return arr; } }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
public void testWithEmptyList2() { List<String> list = new ArrayList<String>(); //this method should not throw IndexOutOfBoundsException String[] arr = GenericsUtil.<String>toArray(String.class, list); assertEquals(0, arr.length); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
T[] funcThatUsesToArray() { T[] arr = GenericsUtil.toArray(list); return arr; } }
public void testToArray() { //test a list of size 10 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(list.get(i), arr[i]); } }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
public void testWithEmptyList2() { List<String> list = new ArrayList<String>(); //this method should not throw IndexOutOfBoundsException String[] arr = GenericsUtil.<String>toArray(String.class, list); assertEquals("Assert list creation w/ no elements results in length 0", 0, arr.length); }
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); //use arr so that compiler will not complain }catch (IndexOutOfBoundsException ex) { //test case is successful } }
public void testWithEmptyList2() { List<String> list = new ArrayList<String>(); //this method should not throw IndexOutOfBoundsException String[] arr = GenericsUtil.<String>toArray(String.class, list); assertEquals("Assert list creation w/ no elements results in length 0", 0, arr.length); }
public void testToArray() { //test a list of size 10 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]); } }
public void testToArray() { //test a list of size 10 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]); } }
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); //use arr so that compiler will not complain }catch (IndexOutOfBoundsException ex) { //test case is successful } }