Java tutorial
//package com.java2s; /* * Copyright (C) 2006 The Android Open Source Project * * 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 { /** * Removes an element from a copy of the array and returns the copy. * If the element is not present, then the original array is returned unmodified. * @param array The original array, or null to represent an empty array. * @param element The element to remove. * @return A new array that contains all of the elements of the original array * except the first copy of the specified element removed. If the specified element * was not present, then returns the original array. Returns null if the result * would be an empty array. */ @SuppressWarnings("unchecked") public static <T> T[] removeElement(final Class<T> kind, final T[] array, final T element) { if (array != null) { final int length = array.length; for (int i = 0; i < length; i++) { if (array[i] == element) { if (length == 1) { return null; } final T[] result = (T[]) Array.newInstance(kind, length - 1); System.arraycopy(array, 0, result, 0, i); System.arraycopy(array, i + 1, result, i, length - i - 1); return result; } } } return array; } }