to Typed Array from Collection - Android java.util

Android examples for java.util:Collection

Description

to Typed Array from Collection

Demo Code

/* /*from   w  w w.  j  av  a 2 s .  c  o m*/
 * Cleandroid Framework 
 * @author: Douraid Arfaoui <douraid.arfaoui@gmail.com>
 *
 * Copyright (c) 2015, Douraid Arfaoui, or third-party contributors as 
 * indicated by the @author tags or express copyright attribution 
 * statements applied by the authors. 
 * 
 * This copyrighted material is made available to anyone wishing to use, modify, 
 * copy, or redistribute it subject to the terms and conditions of the Apache 2 
 * License, as published by the Apache Software Foundation.  
 * 
 */
//package com.java2s;
import java.lang.reflect.Array;

import java.util.Collection;

public class Main {
    public static <T> T[] toTypedArray(Collection<?> collection,
            Class<T> type) {
        @SuppressWarnings("unchecked")
        T[] array = (T[]) Array.newInstance(type, collection.size());
        Object[] data = collection.toArray();
        for (int i = 0; i < array.length; i++) {
            array[i] = type.cast(data[i]);
        }
        return array;

    }
}

Related Tutorials