Java/Collections Data Structure/Array Compare

Материал из Java эксперт
Перейти к: навигация, поиск

Check if a text is present at the current position in a buffer(char array and char array)

   <source lang="java">
  

import java.io.File; import java.io.FileFilter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; /*

*  Licensed to the Apache Software Foundation (ASF) under one
*  or more contributor license agreements.  See the NOTICE file
*  distributed with this work for additional information
*  regarding copyright ownership.  The ASF licenses this file
*  to you 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. 
*  
*/

/**

* Various string manipulation methods that are more efficient then chaining
* string operations: all is done in the same buffer without creating a bunch of
* string objects.
* 
* @author 
*/

public class Main {

 /**
  * Check if a text is present at the current position in a buffer.
  * 
  * @param charArray
  *            The buffer which contains the data
  * @param index
  *            Current position in the buffer
  * @param text
  *            The text we want to check
  * @return true if the buffer contains the text.
  */
 public static final int areEquals( char[] charArray, int index, String text )
 {
     if ( ( charArray == null ) || ( charArray.length == 0 ) || ( charArray.length <= index ) || ( index < 0 )
         || ( text == null ) )
     {
         return -1;
     }
     else
     {
         char[] data = text.toCharArray();
         return areEquals( charArray, index, data );
     }
 }
 /**
  * Check if a text is present at the current position in a buffer.
  * 
  * @param charArray
  *            The buffer which contains the data
  * @param index
  *            Current position in the buffer
  * @param charArray2
  *            The text we want to check
  * @return true if the buffer contains the text.
  */
 public static final int areEquals( char[] charArray, int index, char[] charArray2 )
 {
     if ( ( charArray == null ) || ( charArray.length == 0 ) || ( charArray.length <= index ) || ( index < 0 )
         || ( charArray2 == null ) || ( charArray2.length == 0 )
         || ( charArray2.length > ( charArray.length + index ) ) )
     {
         return -1;
     }
     else
     {
         for ( int i = 0; i < charArray2.length; i++ )
         {
             if ( charArray[index++] != charArray2[i] )
             {
                 return -1;
             }
         }
         return index;
     }
 }

}


 </source>
   
  
 
  



Check if a text is present at the current position in a buffer(char array and string)

   <source lang="java">
  

import java.io.File; import java.io.FileFilter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; /*

*  Licensed to the Apache Software Foundation (ASF) under one
*  or more contributor license agreements.  See the NOTICE file
*  distributed with this work for additional information
*  regarding copyright ownership.  The ASF licenses this file
*  to you 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. 
*  
*/

/**

* Various string manipulation methods that are more efficient then chaining
* string operations: all is done in the same buffer without creating a bunch of
* string objects.
* 
* @author 
*/

public class Main {

 /**
  * Check if a text is present at the current position in a buffer.
  * 
  * @param charArray
  *            The buffer which contains the data
  * @param index
  *            Current position in the buffer
  * @param text
  *            The text we want to check
  * @return true if the buffer contains the text.
  */
 public static final int areEquals( char[] charArray, int index, String text )
 {
     if ( ( charArray == null ) || ( charArray.length == 0 ) || ( charArray.length <= index ) || ( index < 0 )
         || ( text == null ) )
     {
         return -1;
     }
     else
     {
         char[] data = text.toCharArray();
         return areEquals( charArray, index, data );
     }
 }
 /**
  * Check if a text is present at the current position in a buffer.
  * 
  * @param charArray
  *            The buffer which contains the data
  * @param index
  *            Current position in the buffer
  * @param charArray2
  *            The text we want to check
  * @return true if the buffer contains the text.
  */
 public static final int areEquals( char[] charArray, int index, char[] charArray2 )
 {
     if ( ( charArray == null ) || ( charArray.length == 0 ) || ( charArray.length <= index ) || ( index < 0 )
         || ( charArray2 == null ) || ( charArray2.length == 0 )
         || ( charArray2.length > ( charArray.length + index ) ) )
     {
         return -1;
     }
     else
     {
         for ( int i = 0; i < charArray2.length; i++ )
         {
             if ( charArray[index++] != charArray2[i] )
             {
                 return -1;
             }
         }
         return index;
     }
 }

}


 </source>
   
  
 
  



Check if a text is present at the current position in a buffer for byte array

   <source lang="java">
  

import java.io.File; import java.io.FileFilter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; /*

*  Licensed to the Apache Software Foundation (ASF) under one
*  or more contributor license agreements.  See the NOTICE file
*  distributed with this work for additional information
*  regarding copyright ownership.  The ASF licenses this file
*  to you 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. 
*  
*/

/**

* Various string manipulation methods that are more efficient then chaining
* string operations: all is done in the same buffer without creating a bunch of
* string objects.
* 
* @author 
*/

public class Main {

 /**
  * Check if a text is present at the current position in a buffer.
  * 
  * @param byteArray
  *            The buffer which contains the data
  * @param index
  *            Current position in the buffer
  * @param text
  *            The text we want to check
  * @return true if the buffer contains the text.
  */
 public static final int areEquals( byte[] byteArray, int index, String text )
 {
     if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( byteArray.length <= index ) || ( index < 0 )
         || ( text == null ) )
     {
         return -1;
     }
     else
     {
         try
         {
             byte[] data = text.getBytes( "UTF-8" );
             return areEquals( byteArray, index, data );
         }
         catch ( UnsupportedEncodingException uee )
         {
             return -1;
         }
     }
 }
 /**
  * Check if a text is present at the current position in a buffer.
  * 
  * @param byteArray
  *            The buffer which contains the data
  * @param index
  *            Current position in the buffer
  * @param byteArray2
  *            The text we want to check
  * @return true if the buffer contains the text.
  */
 public static final int areEquals( byte[] byteArray, int index, byte[] byteArray2 )
 {
     if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( byteArray.length <= index ) || ( index < 0 )
         || ( byteArray2 == null ) || ( byteArray2.length == 0 )
         || ( byteArray2.length > ( byteArray.length + index ) ) )
     {
         return -1;
     }
     else
     {
         for ( int i = 0; i < byteArray2.length; i++ )
         {
             if ( byteArray[index++] != byteArray2[i] )
             {
                 return -1;
             }
         }
         return index;
     }
 }

}


 </source>
   
  
 
  



Checks whether two arrays are the same length, treating null arrays as length 0.

   <source lang="java">
  

import java.lang.reflect.Array; /*

* Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You 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.
*
*
*/

/**

* @author Stephen Colebourne
* @author Moritz Petersen
* @author 
* @author Maarten Coene
* @since 2.0
* @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
*/

public class Main {

 // Is same length
 //-----------------------------------------------------------------------
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0. * * <p>Any multi-dimensional aspects of the arrays are ignored.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */    
 public static boolean isSameLength(Object[] array1, Object[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */
 public static boolean isSameLength(long[] array1, long[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */
 public static boolean isSameLength(int[] array1, int[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */
 public static boolean isSameLength(short[] array1, short[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */
 public static boolean isSameLength(char[] array1, char[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */
 public static boolean isSameLength(byte[] array1, byte[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */
 public static boolean isSameLength(double[] array1, double[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */
 public static boolean isSameLength(float[] array1, float[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }
 /**
*

Checks whether two arrays are the same length, treating * null arrays as length 0.

  * 
  * @param array1 the first array, may be null
  * @param array2 the second array, may be null
  * @return true if length of arrays matches, treating
  *  null as an empty array
  */
 public static boolean isSameLength(boolean[] array1, boolean[] array2) {
     if ((array1 == null && array2 != null && array2.length > 0) ||
         (array2 == null && array1 != null && array1.length > 0) ||
         (array1 != null && array2 != null && array1.length != array2.length)) {
             return false;
     }
     return true;
 }

}


 </source>
   
  
 
  



Checks whether two arrays are the same type taking into account multi-dimensional arrays.

   <source lang="java">
  

import java.lang.reflect.Array; /*

* Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You 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.
*
*
*/

/**

* @author Stephen Colebourne
* @author Moritz Petersen
* @author 
* @author Maarten Coene
* @since 2.0
* @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
*/

public class Main {

 /**
*

Checks whether two arrays are the same type taking into account * multi-dimensional arrays.

  * 
  * @param array1 the first array, must not be null
  * @param array2 the second array, must not be null
  * @return true if type of arrays matches
  * @throws IllegalArgumentException if either array is null
  */    
 public static boolean isSameType(Object array1, Object array2) {
     if (array1 == null || array2 == null) {
         throw new IllegalArgumentException("The Array must not be null");
     }
     return array1.getClass().getName().equals(array2.getClass().getName());
 }

}


 </source>
   
  
 
  



Compares the initial elements of two arrays.

   <source lang="java">
  

import java.util.Arrays; /*

* JCommon : a free general purpose class library for the Java(tm) platform
* 
*
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
*
* Project Info:  http://www.jfree.org/jcommon/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -------------------
* ArrayUtilities.java
* -------------------
* (C) Copyright 2003-2005, by Object Refinery Limited.
*
* Original Author:  David Gilbert (for Object Refinery Limited);
* Contributor(s):   -;
*
* $Id: ArrayUtilities.java,v 1.7 2008/09/10 09:21:30 mungady Exp $
*
* Changes
* -------
* 21-Aug-2003 : Version 1 (DG);
* 04-Oct-2004 : Renamed ArrayUtils --> ArrayUtilities (DG);
*
*/

public class Main {

 /**
  * Compares the initial elements of two arrays.
  *
  * @param a1  array 1.
  * @param a2  array 2.
  *
  * @return An integer showing the relative ordering.
  */
 public static int compareVersionArrays (Comparable[] a1, Comparable[] a2)
 {
   int length = Math.min (a1.length, a2.length);
   for (int i = 0; i < length; i++)
   {
     Comparable o1 = a1[i];
     Comparable o2 = a2[i];
     if (o1 == null && o2 == null)
     {
       // cannot decide ..
       continue;
     }
     if (o1 == null)
     {
       return 1;
     }
     if (o2 == null)
     {
        return -1;
     }
     int retval = o1.rupareTo(o2);
     if (retval != 0)
     {
       return retval;
     }
   }
   return 0;
 }

}


 </source>
   
  
 
  



Does the source array equal the match array

   <source lang="java">
  

/**********************************************************************************

*
* Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
*                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
*
* Licensed under the Educational Community License Version 1.0 (the "License");
* By obtaining, using and/or copying this Original Work, you agree that you have read,
* understand, and will comply with the terms and conditions of the Educational Community License.
* You may obtain a copy of the License at:
*
*      http://cvs.sakaiproject.org/licenses/license_1_0.html
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**********************************************************************************/

/**

* Byte utilities
*/

public class ByteUtils {

 /**
  * Does the source array equal the match array?
  * 
  * @param source
  *          Byte array to examine
  * @param offset
  *          An offset into the source array
  * @param match
  *          Byte array to locate in source
  * @return true If the two arrays are equal
  */
 public static boolean equals(byte[] source, byte[] match) {
   if (match.length != source.length) {
     return false;
   }
   return startsWith(source, 0, match);
 }
 /**
  * Does this byte array begin with match array content?
  * 
  * @param source
  *          Byte array to examine
  * @param offset
  *          An offset into the source array
  * @param match
  *          Byte array to locate in source
  * @return true If the starting bytes are equal
  */
 public static boolean startsWith(byte[] source, int offset, byte[] match) {
   if (match.length > (source.length - offset)) {
     return false;
   }
   for (int i = 0; i < match.length; i++) {
     if (source[offset + i] != match[i]) {
       return false;
     }
   }
   return true;
 }
 /**
  * Copies bytes from the source byte array to the destination array
  * 
  * @param source
  *          The source array
  * @param srcBegin
  *          Index of the first source byte to copy
  * @param srcEnd
  *          Index after the last source byte to copy
  * @param destination
  *          The destination array
  * @param dstBegin
  *          The starting offset in the destination array
  */
 public static void getBytes(byte[] source, int srcBegin, int srcEnd, byte[] destination,
     int dstBegin) {
   System.arraycopy(source, srcBegin, destination, dstBegin, srcEnd - srcBegin);
 }
 /**
  * Return a new byte array containing a sub-portion of the source array
  * 
  * @param srcBegin
  *          The beginning index (inclusive)
  * @param srcEnd
  *          The ending index (exclusive)
  * @return The new, populated byte array
  */
 public static byte[] subbytes(byte[] source, int srcBegin, int srcEnd) {
   byte destination[];
   destination = new byte[srcEnd - srcBegin];
   getBytes(source, srcBegin, srcEnd, destination, 0);
   return destination;
 }
 /**
  * Return a new byte array containing a sub-portion of the source array
  * 
  * @param srcBegin
  *          The beginning index (inclusive)
  * @return The new, populated byte array
  */
 public static byte[] subbytes(byte[] source, int srcBegin) {
   return subbytes(source, srcBegin, source.length);
 }

}


 </source>
   
  
 
  



Does this byte array begin with match array content?

   <source lang="java">
  

/**********************************************************************************

*
* Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
*                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
*
* Licensed under the Educational Community License Version 1.0 (the "License");
* By obtaining, using and/or copying this Original Work, you agree that you have read,
* understand, and will comply with the terms and conditions of the Educational Community License.
* You may obtain a copy of the License at:
*
*      http://cvs.sakaiproject.org/licenses/license_1_0.html
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**********************************************************************************/

/**

* Byte utilities
*/

public class ByteUtils {

 /**
  * Does this byte array begin with match array content?
  * 
  * @param source
  *          Byte array to examine
  * @param match
  *          Byte array to locate in source
  * @return true If the starting bytes are equal
  */
 public static boolean startsWith(byte[] source, byte[] match) {
   return startsWith(source, 0, match);
 }
 /**
  * Does this byte array begin with match array content?
  * 
  * @param source
  *          Byte array to examine
  * @param offset
  *          An offset into the source array
  * @param match
  *          Byte array to locate in source
  * @return true If the starting bytes are equal
  */
 public static boolean startsWith(byte[] source, int offset, byte[] match) {
   if (match.length > (source.length - offset)) {
     return false;
   }
   for (int i = 0; i < match.length; i++) {
     if (source[offset + i] != match[i]) {
       return false;
     }
   }
   return true;
 }
 /**
  * Does the source array equal the match array?
  * 
  * @param source
  *          Byte array to examine
  * @param offset
  *          An offset into the source array
  * @param match
  *          Byte array to locate in source
  * @return true If the two arrays are equal
  */
 public static boolean equals(byte[] source, byte[] match) {
   if (match.length != source.length) {
     return false;
   }
   return startsWith(source, 0, match);
 }
 /**
  * Copies bytes from the source byte array to the destination array
  * 
  * @param source
  *          The source array
  * @param srcBegin
  *          Index of the first source byte to copy
  * @param srcEnd
  *          Index after the last source byte to copy
  * @param destination
  *          The destination array
  * @param dstBegin
  *          The starting offset in the destination array
  */
 public static void getBytes(byte[] source, int srcBegin, int srcEnd, byte[] destination,
     int dstBegin) {
   System.arraycopy(source, srcBegin, destination, dstBegin, srcEnd - srcBegin);
 }
 /**
  * Return a new byte array containing a sub-portion of the source array
  * 
  * @param srcBegin
  *          The beginning index (inclusive)
  * @param srcEnd
  *          The ending index (exclusive)
  * @return The new, populated byte array
  */
 public static byte[] subbytes(byte[] source, int srcBegin, int srcEnd) {
   byte destination[];
   destination = new byte[srcEnd - srcBegin];
   getBytes(source, srcBegin, srcEnd, destination, 0);
   return destination;
 }
 /**
  * Return a new byte array containing a sub-portion of the source array
  * 
  * @param srcBegin
  *          The beginning index (inclusive)
  * @return The new, populated byte array
  */
 public static byte[] subbytes(byte[] source, int srcBegin) {
   return subbytes(source, srcBegin, source.length);
 }

}


 </source>
   
  
 
  



Returns true if all the references in array1 are equal to all the references in array2 (two null references are considered equal for this test).

   <source lang="java">
  

/*

* JCommon : a free general purpose class library for the Java(tm) platform
* 
*
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
*
* Project Info:  http://www.jfree.org/jcommon/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -------------------
* ArrayUtilities.java
* -------------------
* (C) Copyright 2003-2005, by Object Refinery Limited.
*
* Original Author:  David Gilbert (for Object Refinery Limited);
* Contributor(s):   -;
*
* $Id: ArrayUtilities.java,v 1.7 2008/09/10 09:21:30 mungady Exp $
*
* Changes
* -------
* 21-Aug-2003 : Version 1 (DG);
* 04-Oct-2004 : Renamed ArrayUtils --> ArrayUtilities (DG);
*
*/

public class Main {

 /**
  * Returns true if all the references in array1
  * are equal to all the references in array2 (two
  * null references are considered equal for this test).
  *
  * @param array1  the first array (null permitted).
  * @param array2  the second array (null permitted).
  *
  * @return A boolean.
  */
 public static boolean equalReferencesInArrays(final Object[] array1,
                                               final Object[] array2) {
     if (array1 == null) {
         return (array2 == null);
     }
     if (array2 == null) {
         return false;
     }
     if (array1.length != array2.length) {
         return false;
     }
     for (int i = 0; i < array1.length; i++) {
         if (array1[i] == null) {
             if (array2[i] != null) {
                 return false;
             }
         }
         if (array2[i] == null) {
             if (array1[i] != null) {
                 return false;
             }
         }
         if (array1[i] != array2[i]) {
             return false;
         }
     }
     return true;
 }

}


 </source>
   
  
 
  



Returns true if any two items in the array are equal to one another. Any null values in the array are ignored.

   <source lang="java">
  

import java.util.Arrays; /*

* JCommon : a free general purpose class library for the Java(tm) platform
* 
*
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
*
* Project Info:  http://www.jfree.org/jcommon/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -------------------
* ArrayUtilities.java
* -------------------
* (C) Copyright 2003-2005, by Object Refinery Limited.
*
* Original Author:  David Gilbert (for Object Refinery Limited);
* Contributor(s):   -;
*
* $Id: ArrayUtilities.java,v 1.7 2008/09/10 09:21:30 mungady Exp $
*
* Changes
* -------
* 21-Aug-2003 : Version 1 (DG);
* 04-Oct-2004 : Renamed ArrayUtils --> ArrayUtilities (DG);
*
*/

public class Main {

 /**
  * Returns true if any two items in the array are equal to
  * one another.  Any null values in the array are ignored.
  *
  * @param array  the array to check.
  *
  * @return A boolean.
  */
 public static boolean hasDuplicateItems(final Object[] array) {
     for (int i = 0; i < array.length; i++) {
         for (int j = 0; j < i; j++) {
             final Object o1 = array[i];
             final Object o2 = array[j];
             if (o1 != null && o2 != null) {
                 if (o1.equals(o2)) {
                     return true;
                 }
             }
         }
     }
     return false;
 }

}


 </source>
   
  
 
  



String search and reflection helper methods

   <source lang="java">
   

/**

* The utillib library.
* More information is available at http://www.jinchess.ru/.
* Copyright (C) 2002, 2003 Alexander Maryanovsky.
* All rights reserved.
*
* The utillib library is free software; you can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* The utillib library is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with utillib library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA
*/

import java.util.Hashtable; import java.util.Enumeration; import java.util.NoSuchElementException;

/**

* A collection of general utility methods.
*/

public class Utilities{


 /**
  * A 0-length Object array.
  */
  
 public static final Object [] EMPTY_ARRAY = new Object[0];
 /**
  * A 0-length long array.
  */
  
 public static final long [] EMPTY_LONG_ARRAY = new long[0];
 /**
  * A 0-length int array.
  */
  
 public static final int [] EMPTY_INT_ARRAY = new int[0];
 
 
 /**
  * A 0-length short array.
  */
  
 public static final short [] EMPTY_SHORT_ARRAY = new short[0];
 
 /**
  * A 0-length byte array.
  */
  
 public static final byte [] EMPTY_BYTE_ARRAY = new byte[0];
 
 
 
 /**
  * A 0-length char array.
  */
  
 public static final char [] EMPTY_CHAR_ARRAY = new char[0];
 /**
  * A 0-length double array.
  */
  
 public static final double [] EMPTY_DOUBLE_ARRAY = new double[0];
 
 /**
  * A 0-length float array.
  */
  
 public static final float [] EMPTY_FLOAT_ARRAY = new float[0];
 /**
  * A 0-length String array.
  */
  
 public static final String [] EMPTY_STRING_ARRAY = new String[0];
 
 
 
 /**
  * An empty enumeration.
  */
 
 public static final Enumeration EMPTY_ENUM = new Enumeration(){
   public boolean hasMoreElements(){return false;}
   public Object nextElement(){throw new NoSuchElementException();}
 };
 
 
 /**
  * Returns true if the two specified objects are the same.
  * Returns false otherwise. To be considered the same, the two
  * references must either both be null or invoking equals on one
  * of them with the other must return true.
  */
 public static boolean areEqual(Object obj1, Object obj2){
   return (obj1 == obj2) || (obj1 == null ? false : obj1.equals(obj2));
 }


 /**
  * Maps the specified key to the specified value in the specified
  * Hashtable. If the specified value is null any
  * existing mapping of the specified key is removed from the
  * Hashtable. The old value mapped to the specified key
  * is returned, or null if no value was mapped to the key.
  */
 public static Object put(Hashtable table, Object key, Object value){
   return value == null ? table.remove(key) : table.put(key, value);
 }
 /**
  * Returns true if the specified object is an element of the
  * specified array. The specified array may not be null. The
  * specified object may be null, in which case this method will
  * return true iff one of the indices in the array is empty 
  * (contains null).
  */
 public static boolean contains(Object [] array, Object item){
   return (indexOf(array, item) != -1);
 }
 /**
  * Returns the index of the first occurrance of specified object in the
  * specified array, or -1 if the specified object is not an element of the
  * specified array. The specified object may be null in which
  * case the returned index will be the index of the first null
  * in the array.
  */
 public static int indexOf(Object [] array, Object item){
   if (array == null)
     throw new IllegalArgumentException("The specified array may not be null");
   for (int i = 0; i < array.length; i++)
     if (areEqual(item, array[i]))
       return i;
   return -1;
 }


 /**
  * Returns the index of the first occurrance of specified integer in the
  * specified array, or -1 if the specified integer is not an element of the
  * specified array.
  */
 public static int indexOf(int [] arr, int val){
   if (arr == null)
     throw new IllegalArgumentException("The specified array may not be null");
   for (int i = 0; i < arr.length; i++)
     if (arr[i] == val)
       return i;
   return -1;
 }
 
 
 /**
  * Converts the specified array into a string by appending all its elements
  * separated by a semicolon.
  */
 public static String arrayToString(Object [] arr){
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < arr.length; i++){
     buf.append(arr[i]);
     buf.append("; ");
   }
   if (arr.length > 0)
     buf.setLength(buf.length() - 2); // get rid of the extra "; "
   return buf.toString();
 }


 /**
  * Converts the specified Hashtable into a string by putting
  * each key and value on a separate line (separated by "\n") and an arrow
  * (" -> ") between them.
  */
 public static String hashtableToString(Hashtable hashtable){
   StringBuffer buf = new StringBuffer();
   Enumeration keys = hashtable.keys();
   while (keys.hasMoreElements()){
     Object key = keys.nextElement();
     Object value = hashtable.get(key);
     buf.append(key.toString());
     buf.append(" -> ");
     buf.append(value.toString());
     buf.append("\n");
   }
   return buf.toString();
 }


 /**
  * Returns the maximum element in the specified integer array.
  */
 public static int max(int [] arr){
   if (arr == null)
     throw new IllegalArgumentException("The specified array may not be null");
   if (arr.length == 0)
     throw new IllegalArgumentException("The specified array must have at least one element");
   int n = arr[0];
   for (int i = 1; i < arr.length; i++)
     if (arr[i] > n)
       n = arr[i];
   return n;
 }
 /**
  * Returns a hash code for the specified double value.
  */
 public static int hashCode(double val){
   return hashCode(Double.doubleToLongBits(val));
 }
 /**
  * Returns a hash code for the specified long value.
  */
 public static int hashCode(long val){
   return (int)(val ^ (val >>> 32));
 }
 
 
 
 /**
  * Returns the name of the package of the specified class.
  */
 
 public static String getPackageName(Class c){
   return getPackageName(c.getName());
 }
 
 
 
 /**
  * Returns the name of the package of the class with the specified (full) name.
  */
 
 public static String getPackageName(String className){
   int lastDotIndex = className.lastIndexOf(".");
   return lastDotIndex == -1 ? "" : className.substring(0, lastDotIndex);
 }
 
 
 
 /**
  * Returns the short name (excluding the package name) of the specified class. 
  */
 
 public static String getClassName(Class c){
   return getClassName(c.getName());
 }
 
 
 
 /**
  * Returns the short name (excluding the package name) of the class with the
  * specified fully qualified name.
  */
 
 public static String getClassName(String className){
   int lastDotIndex = className.lastIndexOf(".");
   return lastDotIndex == -1 ? className : className.substring(lastDotIndex + 1);
 }
 
 
 

}



 </source>
   
  
 
  



Tests two float arrays for equality.

   <source lang="java">
  

import java.util.Arrays; /*

* JCommon : a free general purpose class library for the Java(tm) platform
* 
*
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
*
* Project Info:  http://www.jfree.org/jcommon/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -------------------
* ArrayUtilities.java
* -------------------
* (C) Copyright 2003-2005, by Object Refinery Limited.
*
* Original Author:  David Gilbert (for Object Refinery Limited);
* Contributor(s):   -;
*
* $Id: ArrayUtilities.java,v 1.7 2008/09/10 09:21:30 mungady Exp $
*
* Changes
* -------
* 21-Aug-2003 : Version 1 (DG);
* 04-Oct-2004 : Renamed ArrayUtils --> ArrayUtilities (DG);
*
*/

public class Main {

 /**
  * Tests two float arrays for equality.
  *
  * @param array1  the first array (null permitted).
  * @param array2  the second arrray (null permitted).
  *
  * @return A boolean.
  */
 public static boolean equal(final float[][] array1,
                             final float[][] array2) {
     if (array1 == null) {
         return (array2 == null);
     }
     if (array2 == null) {
         return false;
     }
     if (array1.length != array2.length) {
         return false;
     }
     for (int i = 0; i < array1.length; i++) {
         if (!Arrays.equals(array1[i], array2[i])) {
             return false;
         }
     }
     return true;
 }

}


 </source>