Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Array;

import java.util.Collection;

import java.util.List;

import java.util.Vector;

public class Main {
    /**
     * Convert a collection to an array
     * 
     * @param col
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] toArray(Collection<T> col, Class<? super T> c) {
        int size = col.size();
        T[] array = (T[]) Array.newInstance(c, size);
        col.toArray(array);

        return array;
    }

    public static <T> T[] toArray(Iterable<T> it, Class<? super T> c) {
        return toArray(toList(it), c);
    }

    /**
     * Converts an iterable into a list by iterating through the
     * iterable 
     * 
     * @param values
     * @return
     */
    public static <T> List<T> toList(Iterable<T> values) {
        List<T> res = new Vector<>();
        for (T item : values) {
            res.add(item);
        }
        return res;
    }
}