Java tutorial
//package com.java2s; /* * 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. */ import java.lang.reflect.Array; import java.util.Arrays; public class Main { /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll(["a", "b", "c"], 0, 2) = ["b"] * ArrayUtils.removeAll(["a", "b", "c"], 1, 2) = ["a"] * </pre> * * @param <T> the component type of the array * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ @SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input public static <T> T[] removeAll(T[] array, int... indices) { return (T[]) removeAll((Object) array, clone(indices)); } /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll([1], 0) = [] * ArrayUtils.removeAll([2, 6], 0) = [6] * ArrayUtils.removeAll([2, 6], 0, 1) = [] * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2] * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6] * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = [] * </pre> * * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ public static byte[] removeAll(byte[] array, int... indices) { return (byte[]) removeAll((Object) array, clone(indices)); } /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll([1], 0) = [] * ArrayUtils.removeAll([2, 6], 0) = [6] * ArrayUtils.removeAll([2, 6], 0, 1) = [] * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2] * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6] * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = [] * </pre> * * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ public static short[] removeAll(short[] array, int... indices) { return (short[]) removeAll((Object) array, clone(indices)); } /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll([1], 0) = [] * ArrayUtils.removeAll([2, 6], 0) = [6] * ArrayUtils.removeAll([2, 6], 0, 1) = [] * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2] * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6] * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = [] * </pre> * * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ public static int[] removeAll(int[] array, int... indices) { return (int[]) removeAll((Object) array, clone(indices)); } /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll([1], 0) = [] * ArrayUtils.removeAll([2, 6], 0) = [6] * ArrayUtils.removeAll([2, 6], 0, 1) = [] * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2] * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6] * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = [] * </pre> * * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ public static char[] removeAll(char[] array, int... indices) { return (char[]) removeAll((Object) array, clone(indices)); } /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll([1], 0) = [] * ArrayUtils.removeAll([2, 6], 0) = [6] * ArrayUtils.removeAll([2, 6], 0, 1) = [] * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2] * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6] * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = [] * </pre> * * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ public static long[] removeAll(long[] array, int... indices) { return (long[]) removeAll((Object) array, clone(indices)); } /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll([1], 0) = [] * ArrayUtils.removeAll([2, 6], 0) = [6] * ArrayUtils.removeAll([2, 6], 0, 1) = [] * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2] * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6] * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = [] * </pre> * * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ public static float[] removeAll(float[] array, int... indices) { return (float[]) removeAll((Object) array, clone(indices)); } /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll([1], 0) = [] * ArrayUtils.removeAll([2, 6], 0) = [6] * ArrayUtils.removeAll([2, 6], 0, 1) = [] * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2] * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6] * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = [] * </pre> * * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ public static double[] removeAll(double[] array, int... indices) { return (double[]) removeAll((Object) array, clone(indices)); } /** * <p>Removes the elements at the specified positions from the specified array. * All remaining elements are shifted to the left.</p> * * <p>This method returns a new array with the same elements of the input * array except those at the specified positions. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.removeAll([true, false, true], 0, 2) = [false] * ArrayUtils.removeAll([true, false, true], 1, 2) = [true] * </pre> * * @param array the array to remove the element from, may not be {@code null} * @param indices the positions of the elements to be removed * @return A new array containing the existing elements except those * at the specified positions. * @throws IndexOutOfBoundsException if any index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 3.0.1 */ public static boolean[] removeAll(boolean[] array, int... indices) { return (boolean[]) removeAll((Object) array, clone(indices)); } /** * Removes multiple array elements specified by index. * @param array source * @param indices to remove, WILL BE SORTED--so only clones of user-owned arrays! * @return new array of same type minus elements specified by unique values of {@code indices} * @since 3.0.1 */ private static Object removeAll(Object array, int... indices) { int length = getLength(array); int diff = 0; if (isNotEmpty(indices)) { Arrays.sort(indices); int i = indices.length; int prevIndex = length; while (--i >= 0) { int index = indices[i]; if (index < 0 || index >= length) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); } if (index >= prevIndex) { continue; } diff++; prevIndex = index; } } Object result = Array.newInstance(array.getClass().getComponentType(), length - diff); if (diff < length) { int end = length; int dest = length - diff; for (int i = indices.length - 1; i >= 0; i--) { int index = indices[i]; if (end - index > 1) { int cp = end - index - 1; dest -= cp; System.arraycopy(array, index + 1, result, dest, cp); } end = index; } if (end > 0) { System.arraycopy(array, 0, result, 0, end); } } return result; } /** * <p>Shallow clones an array returning a typecast result and handling * {@code null}.</p> * * <p>The objects in the array are not cloned, thus there is no special * handling for multi-dimensional arrays.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param <T> the component type of the array * @param array the array to shallow clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static <T> T[] clone(T[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Clones an array returning a typecast result and handling * {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array the array to clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static long[] clone(long[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Clones an array returning a typecast result and handling * {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array the array to clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static int[] clone(int[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Clones an array returning a typecast result and handling * {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array the array to clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static short[] clone(short[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Clones an array returning a typecast result and handling * {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array the array to clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static char[] clone(char[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Clones an array returning a typecast result and handling * {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array the array to clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static byte[] clone(byte[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Clones an array returning a typecast result and handling * {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array the array to clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static double[] clone(double[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Clones an array returning a typecast result and handling * {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array the array to clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static float[] clone(float[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Clones an array returning a typecast result and handling * {@code null}.</p> * * <p>This method returns {@code null} for a {@code null} input array.</p> * * @param array the array to clone, may be {@code null} * @return the cloned array, {@code null} if {@code null} input */ public static boolean[] clone(boolean[] array) { if (array == null) { return null; } return array.clone(); } /** * <p>Returns the length of the specified array. * This method can deal with {@code Object} arrays and with primitive arrays.</p> * * <p>If the input array is {@code null}, {@code 0} is returned.</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} if the array is {@code null} * @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); } /** * <p>Checks if an array of Objects is not empty or not {@code null}.</p> * * @param <T> the component type of the array * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static <T> boolean isNotEmpty(T[] array) { return (array != null && array.length != 0); } /** * <p>Checks if an array of primitive longs is not empty or not {@code null}.</p> * * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static boolean isNotEmpty(long[] array) { return (array != null && array.length != 0); } /** * <p>Checks if an array of primitive ints is not empty or not {@code null}.</p> * * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static boolean isNotEmpty(int[] array) { return (array != null && array.length != 0); } /** * <p>Checks if an array of primitive shorts is not empty or not {@code null}.</p> * * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static boolean isNotEmpty(short[] array) { return (array != null && array.length != 0); } /** * <p>Checks if an array of primitive chars is not empty or not {@code null}.</p> * * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static boolean isNotEmpty(char[] array) { return (array != null && array.length != 0); } /** * <p>Checks if an array of primitive bytes is not empty or not {@code null}.</p> * * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static boolean isNotEmpty(byte[] array) { return (array != null && array.length != 0); } /** * <p>Checks if an array of primitive doubles is not empty or not {@code null}.</p> * * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static boolean isNotEmpty(double[] array) { return (array != null && array.length != 0); } /** * <p>Checks if an array of primitive floats is not empty or not {@code null}.</p> * * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static boolean isNotEmpty(float[] array) { return (array != null && array.length != 0); } /** * <p>Checks if an array of primitive booleans is not empty or not {@code null}.</p> * * @param array the array to test * @return {@code true} if the array is not empty or not {@code null} * @since 2.5 */ public static boolean isNotEmpty(boolean[] array) { return (array != null && array.length != 0); } }