Inserts an Object into an Object array at the index position. : Array Insert Remove « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Array Insert Remove 




Inserts an Object into an Object array at the index position.
     
import java.lang.reflect.Array;


     /*********************************************************************
     * Array manipulation for Java 1.1+.
     *
     * <p>
     * Java 1.1 compatible.
     * </p>
     *
     @see
     *   ArrayLib2
     *
     @version
     *   2003-04-07
     @since
     *   2001-04-06
     @author
     *   <a href="http://croftsoft.com/">David Wallace Croft</a>*/
public class Util{

    /*********************************************************************
    * Inserts an Object into an Object array at the index position.
    *
    * <p>
    * Example:
    * <code>
    * <pre>
    * String [ ]  stringArray
    *   = ( String [ ] ) ArrayLib.insert ( new String [ ] { }, "", 0 );
    * </pre>
    * </code>
    * </p>
    *
    @throws NullArgumentException
    *
    *   If objectArray or o is null.
    *
    @throws IndexOutOfBoundsException
    *
    *   If index < 0 or index > objectArray.length.
    *
    @return
    *
    *   Returns a new array with the same component type as the old array.
    *********************************************************************/
    public static Object [ ]  insert (
      Object [ ]  objectArray,
      Object      o,
      int         index )
    //////////////////////////////////////////////////////////////////////
    {


      if ( ( index < 0                  )
        || index > objectArray.length ) )
      {
        throw new IndexOutOfBoundsException (
          "index out of range:  " + index );
      }

      Object [ ]  newObjectArray = Object [ ] ) Array.newInstance (
        objectArray.getClass ( ).getComponentType ( ),
        objectArray.length + );


      System.arraycopy objectArray, 0, newObjectArray, 0, index );

      newObjectArray index = o;

      System.arraycopy objectArray, index, newObjectArray, index + 1,
        objectArray.length - index );

      return newObjectArray;
    }

}

   
    
    
    
    
  














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 element at the specified position from the specified long type array.
29.Removes the first occurrence of the specified element from the specified array.
30.Removes the first occurrence of the specified element from the specified long value array.
31.Insert value to array
32.Array Copy Utilities
33.Concatenate Java arrays
34.This program demonstrates array manipulation.
35.Appends an Object to an Object array.
36.Appends an integer to an integer array.
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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.