Convert List to Array - Android java.lang

Android examples for java.lang:array convert

Description

Convert List to Array

Demo Code

import java.lang.reflect.Array;
import java.util.List;

public class Main {

  public static <T extends Object> T[] toArray(List<T> list) {
    if (list == null || list.isEmpty())
      return null;

    T[] array = (T[]) Array.newInstance(list.get(0).getClass(), list.size());
    for (int i = 0; i < array.length; i++)
      array[i] = list.get(i);/*  w  ww  .  ja  va  2  s. c o  m*/
    return array;
  }

  public static <M extends Object> boolean isEmpty(M[] array) {
    return array == null || array.length == 0;
  }

}

Related Tutorials