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

📄 arraylib.java

📁 Java游戏高级编程!!很不错的!!!Java游戏高级编程!!很不错的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         objectArray.length - index );

       return newObjectArray;
     }

     /*********************************************************************
     * Prepends an Object to an Object array.
     *
     * <p>
     * Example:
     * <code>
     * <pre>
     * String [ ]  stringArray
     *   = ( String [ ] ) ArrayLib.prepend ( 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 [ ]  prepend ( 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, 1, oldArray.length );

       newArray [ 0 ] = o;

       return newArray;
     }

     /*********************************************************************
     * Prints each array element to the standard output.
     *********************************************************************/
     public static void  println ( Object [ ]  objectArray )
     //////////////////////////////////////////////////////////////////////
     {
       for ( int  i = 0; i < objectArray.length; i++ )
       {
         System.out.println ( objectArray [ i ] );
       }
     }

     /*********************************************************************
     * Removes an Object from an Object array.
     *
     * <p>
     * Example:
     * <code>
     * <pre>
     * String [ ]  stringArray
     *   = ( String [ ] ) remove ( new String [ ] { "" }, 0 );
     * </pre>
     * </code>
     * </p>
     *
     * @throws NullArgumentException
     *
     *   If oldArray is null.
     *
     * @throws ArrayIndexOutOfBoundsException
     *
     *   If index < 0 or index >= oldArray.length.
     *
     * @return
     *
     *   Returns a new array with the same component type as the old array.
     *********************************************************************/
     public static Object [ ]  remove ( Object [ ]  oldArray, int  index )     //////////////////////////////////////////////////////////////////////     {       NullArgumentException.check ( oldArray );       if ( ( index < 0 )
         || ( index >= oldArray.length ) )
       {
         throw new ArrayIndexOutOfBoundsException ( index );
       }

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

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

       System.arraycopy (
         oldArray, index + 1, newArray, index, newArray.length - index );

       return newArray;
     }

     public static Object [ ]  remove ( Object [ ]  oldArray, Object  o )     //////////////////////////////////////////////////////////////////////     {       NullArgumentException.check ( oldArray );       int  index = -1;       for ( int  i = 0; i < oldArray.length; i++ )       {         if ( oldArray [ i ] == o )         {           index = i;           break;         }       }       if ( index > -1 )       {         return remove ( oldArray, index );       }       return oldArray;     }     /*********************************************************************
     * Removes duplicate elements from the array.
     *********************************************************************/
     public static Object [ ]  removeDuplicates ( Object [ ]  array )
     //////////////////////////////////////////////////////////////////////
     {
       NullArgumentException.check ( array );

       Hashtable  hashtable = new Hashtable ( );

       for ( int  i = 0; i < array.length; i++ )
       {
         hashtable.put ( array [ i ], array [ i ] );
       }

       Object [ ]  newArray = ( Object [ ] ) Array.newInstance (
         array.getClass ( ).getComponentType ( ), hashtable.size ( ) );

       int  index = 0;

       Enumeration  enumeration = hashtable.elements ( );

       while ( enumeration.hasMoreElements ( ) )
       {
         newArray [ index++ ] = enumeration.nextElement ( );
       }

       return newArray;
     }

     /*********************************************************************
     * Creates a new subarray from a larger array.
     *
     * <p>
     * To avoid unnecessary object creation, this method returns the
     * original array argument if the requested subarray length is the same
     * and the startIndex is 0.  That is to say, if the method arguments
     * are such that the algorithm would have created a shallow clone, the
     * original array is returned instead.
     * </p>
     *
     * @throws NullArgumentException
     *
     *   If objectArray is null.
     *
     * @throws ArrayIndexOutOfBoundsException
     *
     *   If startIndex, length, or startIndex + length are out of range.
     *
     * @return
     *
     *   Returns an array with the same component type as the old array.
     *********************************************************************/
     public static Object [ ]  subArray (
       Object [ ]  objectArray,
       int         startIndex,
       int         length )
     //////////////////////////////////////////////////////////////////////
     {
       NullArgumentException.check ( objectArray );

       if ( ( startIndex == 0 )
         && ( length == objectArray.length ) )
       {
         return objectArray;
       }

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

       System.arraycopy ( objectArray, startIndex, newArray, 0, length );

       return newArray;
     }

     /*********************************************************************
     * Creates a new subarray from a larger array.
     *
     * <p>
     * <code>
     * <pre>
     * return subArray (
     *   objectArray, startIndex, objectArray.length - startIndex );
     * </pre>
     * </code>
     * </p>
     *********************************************************************/
     public static Object [ ]  subArray (
       Object [ ]  objectArray,
       int         startIndex )
     //////////////////////////////////////////////////////////////////////
     {
       return subArray (
         objectArray, startIndex, objectArray.length - startIndex );
     }

     /*********************************************************************
     * Returns the union of the arrays, discarding duplicates.
     *********************************************************************/
     public static Object [ ]  union (
       Object [ ]  array1,
       Object [ ]  array2 )
     //////////////////////////////////////////////////////////////////////
     {
       if ( array1 == null )
       {
         if ( array2 == null )
         {
           return null;
         }
         else
         {
           return removeDuplicates ( array2 );
         }
       }
       else if ( array2 == null )
       {
         return removeDuplicates ( array1 );
       }

       Class  componentType1 = array1.getClass ( ).getComponentType ( );

       Class  componentType2 = array2.getClass ( ).getComponentType ( );

       if ( componentType1 != componentType2 )
       {
         throw new IllegalArgumentException (
           "arrays of different component types" );
       }

       Hashtable  hashtable = new Hashtable ( );

       for ( int  i = 0; i < array1.length; i++ )
       {
         hashtable.put ( array1 [ i ], array1 [ i ] );
       }

       for ( int  i = 0; i < array2.length; i++ )
       {
         hashtable.put ( array2 [ i ], array2 [ i ] );
       }

       Object [ ]  array = ( Object [ ] )
         Array.newInstance ( componentType1, hashtable.size ( ) );

       int  index = 0;

       Enumeration  enumeration = hashtable.elements ( );

       while ( enumeration.hasMoreElements ( ) )
       {
         array [ index++ ] = enumeration.nextElement ( );
       }

       return array;
     }

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

     private  ArrayLib ( ) { }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }

⌨️ 快捷键说明

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