Java Array Compare arrayCompare(byte src[], short srcOff, byte dest[], short destOff, short length)

Here you can find the source of arrayCompare(byte src[], short srcOff, byte dest[], short destOff, short length)

Description

Compares an array from the specified source array, beginning at the specified position, with the specified position of the destination array from left to right.

License

Apache License

Parameter

Parameter Description
src source byte array
srcOff offset within source byte array to start compare
dest destination byte array
destOff offset within destination byte array to start compare
length byte length to be compared

Exception

Parameter Description
ArrayIndexOutOfBoundsException if comparing all bytes would cause access of data outside array bounds
NullPointerException if either <code>src</code> or <code>dest</code> is <code>null</code>

Return

the result of the comparison as follows:
  • 0 if identical
  • -1 if the first miscomparing byte in source array is less than that in destination array
  • 1 if the first miscomparing byte in source array is greater that that in destination array

Declaration

public static final byte arrayCompare(byte src[], short srcOff,
        byte dest[], short destOff, short length)
        throws ArrayIndexOutOfBoundsException, NullPointerException 

Method Source Code

//package com.java2s;
/*/*from w w  w  .jav a 2s. com*/
 * Copyright 2011 Licel LLC.
 *
 * Licensed 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.
 */

public class Main {
    /**
     * Compares an array from the specified source array,
     * beginning at the specified position,
     * with the specified position of the destination array from left to right.
     * Returns the ternary result of the comparison : less than(-1), equal(0) or greater than(1).
     * <p>Note:<ul>
     * <li><em>If </em><code>srcOff</code><em> or </em><code>destOff</code><em> or </em><code>length</code><em> parameter
     * is negative an </em><code>ArrayIndexOutOfBoundsException</code><em> exception is thrown.</em>
     * <li><em>If </em><code>srcOff+length</code><em> is greater than </em><code>src.length</code><em>, the length
     * of the </em><code>src</code><em> array a </em><code>ArrayIndexOutOfBoundsException</code><em> exception is thrown.</em>
     * <li><em>If </em><code>destOff+length</code><em> is greater than </em><code>dest.length</code><em>, the length
     * of the </em><code>dest</code><em> array an </em><code>ArrayIndexOutOfBoundsException</code><em> exception is thrown.</em>
     * <li><em>If </em><code>src</code><em> or </em><code>dest</code><em> parameter is </em><code>null</code><em>
     * a </em><code>NullPointerException</code><em> exception is thrown.</em>
     * </ul>
     * @param src source byte array
     * @param srcOff offset within source byte array to start compare
     * @param dest destination byte array
     * @param destOff offset within destination byte array to start compare
     * @param length byte length to be compared
     * @return the result of the comparison as follows:<ul>
     * <li> <code>0</code> if identical</li>
     * <li> <code>-1</code> if the first miscomparing byte in source array is less than that in destination array</li>
     * <li> <code>1</code> if the first miscomparing byte in source array is greater that that in destination array</li>
     * </ul>
     * @throws ArrayIndexOutOfBoundsException if comparing all bytes would cause access of data outside array bounds
     * @throws NullPointerException if either <code>src</code> or <code>dest</code> is <code>null</code>
     */
    public static final byte arrayCompare(byte src[], short srcOff,
            byte dest[], short destOff, short length)
            throws ArrayIndexOutOfBoundsException, NullPointerException {
        if (length < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }
        if (length != 0) {
            byte tester = src[(srcOff + length) - 1];
            tester = dest[(destOff + length) - 1];
        }
        for (short i = 0; i < length; i++) {
            if (src[srcOff + i] != dest[destOff + i]) {
                return ((byte) (src[srcOff + i] >= dest[destOff + i] ? 1
                        : -1));
            }
        }

        return 0;
    }
}

Related

  1. AreEqual(double[] vector1, double[] vector2)
  2. areEqual(final byte[] a, final byte[] b)
  3. areEqualBytes(byte[] b1, byte[] b2)
  4. areEqualPropPaths(String[] pp1, String[] pp2)
  5. areEquals(byte[] g1, byte[] g2)
  6. arrayCompare(byte[] a, byte[] a2)
  7. arrayCompare(byte[] a1, byte[] a2)
  8. arrayCompare(byte[] b1, byte[] b2)
  9. arrayCompare(final T[] a, final T[] b)