Java/Collections Data Structure/Array Compare
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Check if a text is present at the current position in a buffer(char array and char array)
- 2 Check if a text is present at the current position in a buffer(char array and string)
- 3 Check if a text is present at the current position in a buffer for byte array
- 4 Checks whether two arrays are the same length, treating null arrays as length 0.
- 5 Checks whether two arrays are the same type taking into account multi-dimensional arrays.
- 6 Compares the initial elements of two arrays.
- 7 Does the source array equal the match array
- 8 Does this byte array begin with match array content?
- 9 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).
- 10 Returns true if any two items in the array are equal to one another. Any null values in the array are ignored.
- 11 String search and reflection helper methods
- 12 Tests two float arrays for equality.
Check if a text is present at the current position in a buffer(char array and char array)
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 <code>true</code> 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 <code>true</code> 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;
}
}
}
Check if a text is present at the current position in a buffer(char array and string)
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 <code>true</code> 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 <code>true</code> 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;
}
}
}
Check if a text is present at the current position in a buffer for byte array
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 <code>true</code> 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 <code>true</code> 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;
}
}
}
Checks whether two arrays are the same length, treating null arrays as length 0.
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
//-----------------------------------------------------------------------
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* <p>Any multi-dimensional aspects of the arrays are ignored.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
/**
* <p>Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.</p>
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> 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;
}
}
Checks whether two arrays are the same type taking into account multi-dimensional arrays.
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 {
/**
* <p>Checks whether two arrays are the same type taking into account
* multi-dimensional arrays.</p>
*
* @param array1 the first array, must not be <code>null</code>
* @param array2 the second array, must not be <code>null</code>
* @return <code>true</code> if type of arrays matches
* @throws IllegalArgumentException if either array is <code>null</code>
*/
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());
}
}
Compares the initial elements of two arrays.
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;
}
}
Does the source array equal the match array
/**********************************************************************************
*
* 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 <code>source</code> array
* @param match
* Byte array to locate in <code>source</code>
* @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 <code>source</code> array
* @param match
* Byte array to locate in <code>source</code>
* @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);
}
}
Does this byte array begin with match array content?
/**********************************************************************************
*
* 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 <code>source</code>
* @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 <code>source</code> array
* @param match
* Byte array to locate in <code>source</code>
* @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 <code>source</code> array
* @param match
* Byte array to locate in <code>source</code>
* @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);
}
}
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).
/*
* 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 <code>true</code> if all the references in <code>array1</code>
* are equal to all the references in <code>array2</code> (two
* <code>null</code> references are considered equal for this test).
*
* @param array1 the first array (<code>null</code> permitted).
* @param array2 the second array (<code>null</code> 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;
}
}
Returns true if any two items in the array are equal to one another. Any null values in the array are ignored.
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 <code>true</code> if any two items in the array are equal to
* one another. Any <code>null</code> 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;
}
}
String search and reflection helper methods
/**
* 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 <code>true</code> if the two specified objects are the same.
* Returns <code>false</code> otherwise. To be considered the same, the two
* references must either both be null or invoking <code>equals</code> on one
* of them with the other must return <code>true</code>.
*/
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
* <code>Hashtable</code>. If the specified value is <code>null</code> any
* existing mapping of the specified key is removed from the
* <code>Hashtable</code>. The old value mapped to the specified key
* is returned, or <code>null</code> 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 <code>true</code> if the specified object is an element of the
* specified array. The specified array may not be <code>null</code>. The
* specified object may be <code>null</code>, in which case this method will
* return <code>true</code> iff one of the indices in the array is empty
* (contains <code>null</code>).
*/
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 <code>null</code> in which
* case the returned index will be the index of the first <code>null</code>
* 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 <code>Hashtable</code> 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);
}
}
Tests two float arrays for equality.
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 (<code>null</code> permitted).
* @param array2 the second arrray (<code>null</code> 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;
}
}