See if two array slices are the same. - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

See if two array slices are the same.

Demo Code

/**//from   w  w w . ja  v a  2s  .co  m
 * 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.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        char[] left = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c',
                'o', 'm', 'a', '1', };
        int offsetLeft = 2;
        char[] right = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c',
                'o', 'm', 'a', '1', };
        int offsetRight = 2;
        int length = 2;
        System.out.println(equals(left, offsetLeft, right, offsetRight,
                length));
    }

    /**
     * See if two array slices are the same.
     *
     * @param left        The left array to compare
     * @param offsetLeft  The offset into the array.  Must be positive
     * @param right       The right array to compare
     * @param offsetRight the offset into the right array.  Must be positive
     * @param length      The length of the section of the array to compare
     * @return true if the two arrays, starting at their respective offsets, are equal
     * 
     * @see java.util.Arrays#equals(char[], char[])
     */
    public static boolean equals(char[] left, int offsetLeft, char[] right,
            int offsetRight, int length) {
        if ((offsetLeft + length <= left.length)
                && (offsetRight + length <= right.length)) {
            for (int i = 0; i < length; i++) {
                if (left[offsetLeft + i] != right[offsetRight + i]) {
                    return false;
                }

            }
            return true;
        }
        return false;
    }
}

Related Tutorials