Here you can find the source of toArray(final T... items)
Parameter | Description |
---|---|
T | the array's element type |
items | the items of the array |
public static <T> T[] toArray(final T... items)
//package com.java2s; /*// ww w. ja v a 2s.c o m * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Create a type-safe generic array. * * <p>Arrays are covariant i.e. they cannot be created from a generic type:</p> * * <pre> public static <T> T[] createAnArray(int size) { return T[size]; // compiler error here } public static <T> T[] createAnArray(int size) { return (T[])Object[size]; // ClassCastException at runtime } * </pre> * * <p>Therefore new arrays of generic types can be created with this method, e.g. an arrays * of Strings:</p> * * <pre> String[] array = ArrayUtils.toArray("1", "2"); String[] emptyArray = ArrayUtils.<String>toArray(); * </pre> * * The method is typically used in scenarios, where the caller itself uses generic types * that have to be combined into an array. * * Note, this method makes only sense to provide arguments of the same type so that the * compiler can deduce the type of the array itself. While it is possible to select the * type explicitly like in <code>Number[] array = ArrayUtils.<Number>toArray(new * Integer(42), new Double(Math.PI))</code>, there is no real advantage to <code>new * Number[] {new Integer(42), new Double(Math.PI)}</code> anymore. * * @param <T> the array's element type * @param items the items of the array * @return the array * @since 3.0 */ public static <T> T[] toArray(final T... items) { return items; } }