Java tutorial
//package com.java2s; /* * Copyright (c) 2013-2015 Jeff Sutton. * * 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 { /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, null) = [null] * ArrayUtils.add(null, "a") = ["a"] * ArrayUtils.add(["a"], null) = ["a", null] * ArrayUtils.add(["a"], "b") = ["a", "b"] * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"] * </pre> * * @param array the array to "add" the element to, may be <code>null</code> * @param element the object to add * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static Object[] add(Object[] array, Object element) { Class type = array != null ? array.getClass() : (element != null ? element.getClass() : Object.class); Object[] newArray = (Object[]) copyArrayGrow1(array, type); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, true) = [true] * ArrayUtils.add([true], false) = [true, false] * ArrayUtils.add([true, false], true) = [true, false, true] * </pre> * * @param array the array to copy and add the element to, may be <code>null</code> * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static boolean[] add(boolean[] array, boolean element) { boolean[] newArray = (boolean[]) copyArrayGrow1(array, Boolean.TYPE); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0) = [0] * ArrayUtils.add([1], 0) = [1, 0] * ArrayUtils.add([1, 0], 1) = [1, 0, 1] * </pre> * * @param array the array to copy and add the element to, may be <code>null</code> * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static byte[] add(byte[] array, byte element) { byte[] newArray = (byte[]) copyArrayGrow1(array, Byte.TYPE); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, '0') = ['0'] * ArrayUtils.add(['1'], '0') = ['1', '0'] * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1'] * </pre> * * @param array the array to copy and add the element to, may be <code>null</code> * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static char[] add(char[] array, char element) { char[] newArray = (char[]) copyArrayGrow1(array, Character.TYPE); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0) = [0] * ArrayUtils.add([1], 0) = [1, 0] * ArrayUtils.add([1, 0], 1) = [1, 0, 1] * </pre> * * @param array the array to copy and add the element to, may be <code>null</code> * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static double[] add(double[] array, double element) { double[] newArray = (double[]) copyArrayGrow1(array, Double.TYPE); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0) = [0] * ArrayUtils.add([1], 0) = [1, 0] * ArrayUtils.add([1, 0], 1) = [1, 0, 1] * </pre> * * @param array the array to copy and add the element to, may be <code>null</code> * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static float[] add(float[] array, float element) { float[] newArray = (float[]) copyArrayGrow1(array, Float.TYPE); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0) = [0] * ArrayUtils.add([1], 0) = [1, 0] * ArrayUtils.add([1, 0], 1) = [1, 0, 1] * </pre> * * @param array the array to copy and add the element to, may be <code>null</code> * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static int[] add(int[] array, int element) { int[] newArray = (int[]) copyArrayGrow1(array, Integer.TYPE); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0) = [0] * ArrayUtils.add([1], 0) = [1, 0] * ArrayUtils.add([1, 0], 1) = [1, 0, 1] * </pre> * * @param array the array to copy and add the element to, may be <code>null</code> * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static long[] add(long[] array, long element) { long[] newArray = (long[]) copyArrayGrow1(array, Long.TYPE); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Copies the given array and adds the given element at the end of the new array.</p> * <p> * <p>The new array contains the same elements of the input * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0) = [0] * ArrayUtils.add([1], 0) = [1, 0] * ArrayUtils.add([1, 0], 1) = [1, 0, 1] * </pre> * * @param array the array to copy and add the element to, may be <code>null</code> * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element * @since 2.1 */ public static short[] add(short[] array, short element) { short[] newArray = (short[]) copyArrayGrow1(array, Short.TYPE); newArray[newArray.length - 1] = element; return newArray; } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0, null) = [null] * ArrayUtils.add(null, 0, "a") = ["a"] * ArrayUtils.add(["a"], 1, null) = ["a", null] * ArrayUtils.add(["a"], 1, "b") = ["a", "b"] * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static Object[] add(Object[] array, int index, Object element) { Class clss = null; if (array != null) { clss = array.getClass().getComponentType(); } else if (element != null) { clss = element.getClass(); } else { return new Object[] { null }; } return (Object[]) add(array, index, element, clss); } /** * Underlying implementation of add(array, index, element) methods. * The last parameter is the class, which may not equal element.getClass * for primitives. * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @param clss the type of the element being added * @return A new array containing the existing elements and the new element */ private static Object add(Object array, int index, Object element, Class clss) { if (array == null) { if (index != 0) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0"); } Object joinedArray = Array.newInstance(clss, 1); Array.set(joinedArray, 0, element); return joinedArray; } int length = Array.getLength(array); if (index > length || index < 0) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); } Object result = Array.newInstance(clss, length + 1); System.arraycopy(array, 0, result, 0, index); Array.set(result, index, element); if (index < length) { System.arraycopy(array, index, result, index + 1, length - index); } return result; } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0, true) = [true] * ArrayUtils.add([true], 0, false) = [false, true] * ArrayUtils.add([false], 1, true) = [false, true] * ArrayUtils.add([true, false], 1, true) = [true, true, false] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static boolean[] add(boolean[] array, int index, boolean element) { return (boolean[]) add(array, index, Boolean.valueOf(element), Boolean.TYPE); } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add(null, 0, 'a') = ['a'] * ArrayUtils.add(['a'], 0, 'b') = ['b', 'a'] * ArrayUtils.add(['a', 'b'], 0, 'c') = ['c', 'a', 'b'] * ArrayUtils.add(['a', 'b'], 1, 'k') = ['a', 'k', 'b'] * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c'] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static char[] add(char[] array, int index, char element) { return (char[]) add(array, index, Character.valueOf(element), Character.TYPE); } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add([1], 0, 2) = [2, 1] * ArrayUtils.add([2, 6], 2, 3) = [2, 6, 3] * ArrayUtils.add([2, 6], 0, 1) = [1, 2, 6] * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static byte[] add(byte[] array, int index, byte element) { return (byte[]) add(array, index, Byte.valueOf(element), Byte.TYPE); } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add([1], 0, 2) = [2, 1] * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10] * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6] * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static short[] add(short[] array, int index, short element) { return (short[]) add(array, index, element, Short.TYPE); } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add([1], 0, 2) = [2, 1] * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10] * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6] * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static int[] add(int[] array, int index, int element) { return (int[]) add(array, index, Integer.valueOf(element), Integer.TYPE); } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add([1L], 0, 2L) = [2L, 1L] * ArrayUtils.add([2L, 6L], 2, 10L) = [2L, 6L, 10L] * ArrayUtils.add([2L, 6L], 0, -4L) = [-4L, 2L, 6L] * ArrayUtils.add([2L, 6L, 3L], 2, 1L) = [2L, 6L, 1L, 3L] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static long[] add(long[] array, int index, long element) { return (long[]) add(array, index, Long.valueOf(element), Long.TYPE); } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add([1.1f], 0, 2.2f) = [2.2f, 1.1f] * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f) = [2.3f, 6.4f, 10.5f] * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f) = [-4.8f, 2.6f, 6.7f] * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f) = [2.9f, 6.0f, 1.0f, 0.3f] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static float[] add(float[] array, int index, float element) { return (float[]) add(array, index, Float.valueOf(element), Float.TYPE); } /** * <p>Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).</p> * <p> * <p>This method returns a new array with the same elements of the input * array plus the given element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * <p> * <p>If the input array is <code>null</code>, a new one element array is returned * whose component type is the same as the element.</p> * <p> * <pre> * ArrayUtils.add([1.1], 0, 2.2) = [2.2, 1.1] * ArrayUtils.add([2.3, 6.4], 2, 10.5) = [2.3, 6.4, 10.5] * ArrayUtils.add([2.6, 6.7], 0, -4.8) = [-4.8, 2.6, 6.7] * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0) = [2.9, 6.0, 1.0, 0.3] * </pre> * * @param array the array to add the element to, may be <code>null</code> * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static double[] add(double[] array, int index, double element) { return (double[]) add(array, index, Double.valueOf(element), Double.TYPE); } /** * Returns a copy of the given array of size 1 greater than the argument. * The last value of the array is left to the default value. * * @param array The array to copy, must not be <code>null</code>. * @param newArrayComponentType If <code>array</code> is <code>null</code>, create a * size 1 array of this type. * @return A new copy of the array of size 1 greater than the input. */ private static Object copyArrayGrow1(Object array, Class newArrayComponentType) { if (array != null) { int arrayLength = Array.getLength(array); Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1); System.arraycopy(array, 0, newArray, 0, arrayLength); return newArray; } return Array.newInstance(newArrayComponentType, 1); } /** * <p>Returns the length of the specified array. * This method can deal with <code>Object</code> arrays and with primitive arrays.</p> * <p> * <p>If the input array is <code>null</code>, <code>0</code> is returned.</p> * <p> * <pre> * ArrayUtils.getLength(null) = 0 * ArrayUtils.getLength([]) = 0 * ArrayUtils.getLength([null]) = 1 * ArrayUtils.getLength([true, false]) = 2 * ArrayUtils.getLength([1, 2, 3]) = 3 * ArrayUtils.getLength(["a", "b", "c"]) = 3 * </pre> * * @param array the array to retrieve the length from, may be null * @return The length of the array, or <code>0</code> if the array is <code>null</code> * @throws IllegalArgumentException if the object arguement is not an array. * @since 2.1 */ public static int getLength(Object array) { if (array == null) { return 0; } return Array.getLength(array); } }