Java tutorial
/* * Copyright (C) 2008 feilong * * 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. */ package com.feilong.core.lang; import java.lang.reflect.Array; import org.apache.commons.lang3.Validate; /** * . * * <p> * detect array,object is an instanceof boolean[], byte[], short[], char[], int[], long[], float[], double[], or * Object[], * * <br> * :Object[] Integer/String...User. instanceof Object[],?primitive ,instanceof Object[];<br> * so depending on how you want to handle nested arrays, it can get complicated. * </p> * * <h3>??:</h3> * <blockquote> * <ol> * <li>{@link #getElement(Object, int)},?</li> * </ol> * </blockquote> * * * <h3>??:</h3> * * <blockquote> * <ul> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(boolean[], boolean)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(byte[], byte)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(char[], char)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(double[], double)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(float[], float)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(int[], int)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(long[], long)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(Object[], Object)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(short[], short)}</li> * <li>{@link org.apache.commons.lang3.ArrayUtils#contains(double[], double, double)}</li> * </ul> * </blockquote> * * @author <a href="http://feitianbenyue.iteye.com/">feilong</a> * @see org.apache.commons.lang3.ArrayUtils * @since 1.4.0 */ public final class ArrayUtil { /** Don't let anyone instantiate this class. */ private ArrayUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } /** * <code>array</code> <code>index</code> . * * <p> * (Returns the value of the indexed component in the specified array object. <br> * The value is automatically wrapped in an object if it has a primitive type.) * </p> * * <pre class="code"> * * Example 1: * * Object array = new String[] { "jinxin", "feilong", "1" }; * LOGGER.info("" + ArrayUtil.getElement(array, 2)); * * :1 * </pre> * * @param <T> * the generic type * @param array * * @param index * * @return ?{@code index}, <code>array</code> , {@link ArrayIndexOutOfBoundsException} * @see java.lang.reflect.Array#get(Object, int) */ @SuppressWarnings("unchecked") public static <T> T getElement(Object array, int index) { return (T) Array.get(array, index); } /** * <code>componentType</code> ? <code>length</code>. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * assertArrayEquals(new Integer[] {}, ArrayUtil.newArray(Integer.class, 0)); * assertArrayEquals(new Integer[] { null, null, null }, ArrayUtil.newArray(Integer.class, 3)); * </pre> * * </blockquote> * * @param <T> * the generic type * @param componentType * the component type * @param length * the length of the new array * @return <code>componentType</code> null, {@link NullPointerException}<br> * {@code length < 0} , {@link IllegalArgumentException}<br> * @see java.lang.reflect.Array#newInstance(Class, int) * @see java.lang.reflect.Array#newInstance(Class, int...) * @see "com.google.common.collect#newArray(Class, int)" * @since 1.6.1 */ @SuppressWarnings("unchecked") public static <T> T[] newArray(Class<T> componentType, int length) { Validate.notNull(componentType, "componentType can't be null!"); Validate.isTrue(length >= 0, "length:[%s],must >=0", length); return (T[]) Array.newInstance(componentType, length); } }