Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* Copyright 2005-2006 Tim Fennell
 *
 * Licensed 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.
 */

import java.lang.reflect.Array;

public class Main {
    /**
     * Converts an Object reference that is known to be an array into an
     * Object[]. If the array is assignable to Object[], the array passed in is
     * simply cast and returned. Otherwise a new Object[] of equal size is
     * constructed and the elements are wrapped and inserted into the new array
     * before being returned.
     * 
     * @param in
     *            an array of Objects or primitives
     * @return an Object[], either the array passed in, or in the case of
     *         primitives, a new Object[] containing a wrapper for each element
     *         in the input array
     * @throws IllegalArgumentException
     *             thrown if the in parameter is null or not an array
     */
    public static Object[] asObjectArray(Object in) {
        if (in == null || !in.getClass().isArray()) {
            throw new IllegalArgumentException("Parameter to asObjectArray must be a non-null array.");
        } else if (in instanceof Object[]) {
            return (Object[]) in;
        } else {
            int length = Array.getLength(in);
            Object[] out = new Object[length];
            for (int i = 0; i < length; ++i) {
                out[i] = Array.get(in, i);
            }

            return out;
        }
    }
}