to Array via Array.newInstance - Android java.lang

Android examples for java.lang:array

Description

to Array via Array.newInstance

Demo Code

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

public class Main{

    public static <T> T[] toArray(List<T> list, Class<T> clazz) {
        @SuppressWarnings("unchecked")
        T[] result = (T[]) Array.newInstance(clazz, list.size());
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);//from ww w  .j av a  2s .  co m
        }
        return result;
    }

}

Related Tutorials