Converting a list into an array


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public static <T> T[] convertToArray(List<T> values, Class<T> clazz) {
  2. Object object = Array.newInstance(clazz, values.size());
  3. for (int i = 0; i < values.size(); i++) {
  4. Array.set(object, i, values.get(i));
  5. }
  6.  
  7. @SuppressWarnings("unchecked")
  8. T[] array = (T[]) object;
  9. return array;
  10. }

URL: http://download.oracle.com/javase/tutorial/reflect/special/arrayInstance.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.