Returns the argument, cast to type T[] in an unsafe way. - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

Returns the argument, cast to type T[] in an unsafe way.

Demo Code

/**// ww  w .j a va  2 s. c  om
 * Copyright (c) 2010 Martin Geisse
 *
 * This file is distributed under the terms of the MIT license.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object[] a = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(java.util.Arrays.toString(unsafeCastArray(a)));
    }

    /**
     * Returns the argument, cast to type T[] in an unsafe way.
     * @param <T> the static element type of the array type to cast to
     * @param a the value to cast
     * @return Returns the argument, cast to type T[].
     */
    @SuppressWarnings("unchecked")
    private static <T> T[] unsafeCastArray(Object[] a) {
        return (T[]) a;
    }
}

Related Tutorials