⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arraylib.java

📁 Java游戏高级编程!!很不错的!!!Java游戏高级编程!!很不错的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     package com.croftsoft.core.util;

     import java.lang.reflect.Array;
     import java.util.Enumeration;
     import java.util.Hashtable;

     import com.croftsoft.core.lang.NullArgumentException;
     import com.croftsoft.core.lang.ObjectLib;

     /*********************************************************************
     * 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 final class  ArrayLib
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     {

     public static void  main ( String [ ]  args )
     //////////////////////////////////////////////////////////////////////
     {
       System.out.println ( test ( args ) );
     }

     public static boolean  test ( String [ ]  args )
     //////////////////////////////////////////////////////////////////////
     {
       try
       {
         insert ( new int [ ] { }, 0, 0 );

         String [ ]  stringArray
           = ( String [ ] ) append ( new String [ ] { }, "" );

         stringArray
           = ( String [ ] ) insert ( new String [ ] { }, "", 0 );

         stringArray
           = ( String [ ] ) remove ( new String [ ] { "" }, 0 );

         return true;
       }
       catch ( Exception  ex )
       {
         ex.printStackTrace ( );

         return false;
       }
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     /*********************************************************************
     * Appends an Object to an Object array.
     *
     * <p>
     * Example:
     * <code>
     * <pre>
     * String [ ]  stringArray
     *   = ( String [ ] ) ArrayLib.append ( new String [ ] { }, "" );
     * </pre>
     * </code>
     * </p>
     *
     * @throws NullArgumentException
     *
     *   If either argument is null.
     *
     * @return
     *
     *   Returns a new array with the same component type as the old array.
     *********************************************************************/
     public static Object [ ]  append ( Object [ ]  oldArray, Object  o )
     //////////////////////////////////////////////////////////////////////
     {
       NullArgumentException.check ( oldArray );

       NullArgumentException.check ( o );

       Object [ ]  newArray = ( Object [ ] ) Array.newInstance (
         oldArray.getClass ( ).getComponentType ( ), oldArray.length + 1 );

       System.arraycopy ( oldArray, 0, newArray, 0, oldArray.length );

       newArray [ oldArray.length ] = o;

       return newArray;
     }

     /*********************************************************************
     * Appends an integer to an integer array.
     *
     * @param  intArray
     *
     *   May be null.
     *********************************************************************/
     public static int [ ]  append ( int [ ]  intArray, int  i )
     //////////////////////////////////////////////////////////////////////
     {
       if ( intArray == null )
       {
         return new int [ ] { i };
       }

       int  intArrayLength = intArray.length;

       int [ ]  newIntArray = new int [ intArrayLength + 1 ];

       System.arraycopy ( intArray, 0, newIntArray, 0, intArrayLength );

       newIntArray [ intArrayLength ] = i;

       return newIntArray;
     }

     /*********************************************************************
     * Determines if an array contains an equivalent object.
     *
     * @param  objectArray
     *
     *   May not be null.
     *
     * @param  o
     *
     *   If null, function will return true if an array element is null.
     *********************************************************************/
     public static boolean  contains (
       Object [ ]  objectArray,
       Object      o )
     //////////////////////////////////////////////////////////////////////
     {
       NullArgumentException.check ( objectArray );

       for ( int  i = 0; i < objectArray.length; i++ )
       {
         if ( ObjectLib.equivalent ( objectArray [ i ], o ) )
         {
           return true;
         }
       }

       return false;
     }

     /*********************************************************************
     * Compares two object arrays for equivalency.
     *
     * <p>
     * A Java 1.1 version of the Java 1.2 method java.util.Arrays.equals().
     * </p>
     *********************************************************************/
     public static boolean  equals (
       Object [ ]  objectArray1,
       Object [ ]  objectArray2 )
     //////////////////////////////////////////////////////////////////////
     {
       if ( objectArray1 == null )
       {
         return objectArray2 == null;
       }
       else if ( objectArray2 == null )
       {
         return false;
       }

       if ( objectArray1.length != objectArray2.length )
       {
         return false;
       }

       for ( int  i = 0; i < objectArray1.length; i++ )
       {
         Object  element1 = objectArray1 [ i ];

         Object  element2 = objectArray2 [ i ];

         if ( element1 == null )
         {
           if ( element2 != null )
           {
             return false;
           }
         }
         else if ( !element1.equals ( element2 ) )
         {
           return false;         
         }
       }

       return true;
     }

     /*********************************************************************
     * Inserts an integer into an integer array at the index position.
     *********************************************************************/
     public static int [ ]  insert (
       int [ ]  intArray,
       int      i,
       int      index )
     //////////////////////////////////////////////////////////////////////
     {
       NullArgumentException.check ( intArray );

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

       int  intArrayLength = intArray.length;

       int [ ]  newIntArray = new int [ intArrayLength + 1 ];

       System.arraycopy ( intArray, 0, newIntArray, 0, index );

       newIntArray [ index ] = i;

       System.arraycopy (
         intArray, index, newIntArray, index + 1, intArrayLength - index );

       return newIntArray;
     }

     /*********************************************************************
     * 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 )
     //////////////////////////////////////////////////////////////////////
     {
       NullArgumentException.check ( objectArray );

       NullArgumentException.check ( o );

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

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


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

       newObjectArray [ index ] = o;

       System.arraycopy ( objectArray, index, newObjectArray, index + 1,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -