Removes the element at the specified position from the specified long type array. : Array Insert Remove « Collections Data Structure « Java






Removes the element at the specified position from the specified long type array.

      
/*   Copyright 2004 The Apache Software Foundation
 *
 *   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;


/**
 * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
 * primitive wrapper arrays (like <code>Integer[]</code>).</p>
 * 
 * <p>This class tries to handle <code>null</code> input gracefully.
 * An exception will not be thrown for a <code>null</code>
 * array input. However, an Object array that contains a <code>null</code>
 * element may throw an exception. Each method documents its behaviour.</p>
 *
 * @author Stephen Colebourne
 * @author Moritz Petersen
 * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
 * @author Nikolay Metchev
 * @author Matthew Hawthorne
 * @author Tim O'Brien
 * @author Pete Gieser
 * @author Gary Gregory
 * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
 * @author Maarten Coene
 * @since 2.0
 * @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
 */
public class Main {

  
  /**
   * <p>Removes the element at the specified position from the specified array.
   * All subsequent elements are shifted to the left (substracts one from
   * their indices).</p>
   *
   * <p>This method returns a new array with the same elements of the input
   * array except the element on the specified position. 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</code>, an IndexOutOfBoundsException
   * will be thrown, because in that case no valid index can be specified.</p>
   *
   * <pre>
   * ArrayUtils.remove([1], 0)         = []
   * ArrayUtils.remove([2, 6], 0)      = [6]
   * ArrayUtils.remove([2, 6], 1)      = [2]
   * ArrayUtils.remove([2, 6, 3], 1)   = [2, 3]
   * </pre>
   * 
   * @param array  the array to remove the element from, may not be <code>null</code>
   * @param index  the position of the element to be removed
   * @return A new array containing the existing elements except the element
   *         at the specified position.
   * @throws IndexOutOfBoundsException if the index is out of range 
   * (index < 0 || index >= array.length), or if the array is <code>null</code>.
   * @since 2.1
   */
  public static long[] remove(long[] array, int index) {
      return (long[]) remove((Object) array, index);
  }
  
  /**
   * <p>Removes the first occurrence of the specified element from the
   * specified array. All subsequent elements are shifted to the left 
   * (substracts one from their indices). If the array doesn't contains
   * such an element, no elements are removed from the array.</p>
   *
   * <p>This method returns a new array with the same elements of the input
   * array except the first occurrence of the specified element. The component 
   * type of the returned array is always the same as that of the input 
   * array.</p>
   *
   * <pre>
   * ArrayUtils.removeElement(null, 1)      = null
   * ArrayUtils.removeElement([], 1)        = []
   * ArrayUtils.removeElement([1], 2)       = [1]
   * ArrayUtils.removeElement([1, 3], 1)    = [3]
   * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
   * </pre>
   * 
   * @param array  the array to remove the element from, may be <code>null</code>
   * @param element  the element to be removed
   * @return A new array containing the existing elements except the first
   *         occurrence of the specified element.
   * @since 2.1
   */
  public static long[] removeElement(long[] array, long element) {
      int index = indexOf(array, element);
      if (index == -1) {
          return clone(array);
      } 
      return remove(array, index);
  }
  /**
   * <p>Removes the element at the specified position from the specified array.
   * All subsequent elements are shifted to the left (substracts one from
   * their indices).</p>
   *
   * <p>This method returns a new array with the same elements of the input
   * array except the element on the specified position. 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</code>, an IndexOutOfBoundsException
   * will be thrown, because in that case no valid index can be specified.</p>
   * 
   * @param array  the array to remove the element from, may not be <code>null</code>
   * @param index  the position of the element to be removed
   * @return A new array containing the existing elements except the element
   *         at the specified position.
   * @throws IndexOutOfBoundsException if the index is out of range 
   * (index < 0 || index >= array.length), or if the array is <code>null</code>.
   * @since 2.1
   */
  private static Object remove(Object array, int index) {
      int length = getLength(array);
      if (index < 0 || index >= length) {
          throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
      }
      
      Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
      System.arraycopy(array, 0, result, 0, index);
      if (index < length - 1) {
          System.arraycopy(array, index + 1, result, index, length - index - 1);
      }
      
      return result;
  }
  /**
   * <p>Finds the index of the given value in the array starting at the given index.</p>
   *
   * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
   *
   * <p>A negative startIndex is treated as zero. A startIndex larger than the array
   * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
   * 
   * @param array  the array to search through for the object, may be <code>null</code>
   * @param valueToFind  the value to find
   * @param startIndex  the index to start searching at
   * @return the index of the value within the array,
   *  {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
   */
  public static int indexOf(long[] array, long valueToFind) {
      if (array == null) {
          return -1;
      }
      for (int i = 0; i < array.length; i++) {
          if (valueToFind == array[i]) {
              return i;
          }
      }
      return -1;
  }
  /**
   * <p>Returns the length of the specified array.
   * This method can deal with <code>Object</code> arrays and with primitive arrays.</p>
   *
   * <p>If the input array is <code>null</code>, <code>0</code> 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</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);
  }
  /**
   * <p>Shallow clones an array returning a typecast result and handling
   * <code>null</code>.</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</code> for a <code>null</code> input array.</p>
   * 
   * @param array  the array to shallow clone, may be <code>null</code>
   * @return the cloned array, <code>null</code> if <code>null</code> input
   */
  public static long[] clone(long[] array) {
      if (array == null) {
          return null;
      }
      return (long[]) array.clone();
  }



}

   
    
    
    
    
    
  








Related examples in the same category

1.Adds all the elements of the given arrays into a new boolean-value array.
2.Adds all the elements of the given arrays into a new byte-type array.
3.Adds all the elements of the given arrays into a new char-type array.
4.Adds all the elements of the given arrays into a new double-type array.
5.Adds all the elements of the given arrays into a new float-type array.
6.Adds all the elements of the given arrays into a new int-type array.
7.Adds all the elements of the given arrays into a new long-type array.
8.Adds all the elements of the given arrays into a new short-type array.
9.Copies the given array and adds the given element at the end of the new array. (char type value)
10.Copies the given array and adds the given element at the end of the new array. (float type value)
11.Copies the given array and adds the given element at the end of the new array. (long value type)
12.Copies the given array and adds the given element at the end of the new array. (object value type)
13.Copies the given array and adds the given element at the end of the new array.(boolean value type)
14.Copies the given array and adds the given element at the end of the new array.(byte value type)
15.Copies the given array and adds the given element at the end of the new array.(double type value)
16.Copies the given array and adds the given element at the end of the new array.(int value type)
17.Copies the given array and adds the given element at the end of the new array.(short type array)
18.Inserts the specified element at the specified position in the array.
19.Inserts the specified element at the specified position in the boolean-type-value array.
20.Inserts the specified element at the specified position in the byte-type-value array.
21.Inserts the specified element at the specified position in the char-type-value array.
22.Inserts the specified element at the specified position in the double-type-value array.
23.Inserts the specified element at the specified position in the float-value-type array.
24.Inserts the specified element at the specified position in the int-type-value array.
25.Inserts the specified element at the specified position in the long-type-value array.
26.Inserts the specified element at the specified position in the short-value-type array.
27.Removes the element at the specified position from the specified array.
28.Removes the first occurrence of the specified element from the specified array.
29.Removes the first occurrence of the specified element from the specified long value array.
30.Insert value to array
31.Array Copy Utilities
32.Concatenate Java arrays
33.This program demonstrates array manipulation.
34.Appends an Object to an Object array.
35.Appends an integer to an integer array.
36.Inserts an Object into an Object array at the index position.
37.Prepends an Object to an Object array.
38.Removes an Object from an Object array.
39.Removes duplicate elements from the array
40.Creates a new subarray from a larger array.
41.Append one array to another
42.Array Expander
43.Array Util: seach, insert, append, remove, copy, shuffle
44.Add new value to exsisting array.The new value is indexed to the last.
45.Add new value, which sets as the first value, to existing array.
46.Add one array to another
47.Concatenates all the passed arrays
48.Returns a copy of the specified array of objects of the specified size.
49.Pack chunks
50.Removes an element from a an array yielding a new array with that data.