Returns if the given int arrays have the same length and contain the same values in the same order - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

Returns if the given int arrays have the same length and contain the same values in the same order

Demo Code

/*//from   ww  w. j  a  va 2 s  .c o m
  You may freely copy, distribute, modify and use this class as long
  as the original author attribution remains intact.  See message
  below.

  Copyright (C) 2003 Christian Pesch. All Rights Reserved.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] first = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int[] second = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(equals(first, second));
    }

    /**
     * Returns if the given int arrays have the same length
     * and contain the same values in the same order
     *
     * @param first  the first array to compare
     * @param second the second array to compare
     * @return true if the given int arrays have the same length
     *         and contain the same values in the same order
     */
    public static boolean equals(int[] first, int[] second) {
        if (first == second)
            return true;

        if (first == null || second == null)
            return false;

        if (first.length != second.length)
            return false;

        for (int i = 0; i < second.length; i++) {
            if (first[i] != second[i])
                return false;
        }
        return true;
    }

    /**
     * Returns if the given byte arrays have the same length
     * and contain the same values in the same order
     *
     * @param first  the first array to compare
     * @param second the second array to compare
     * @return true if the given byte arrays have the same length
     *         and contain the same values in the same order
     */
    public static boolean equals(byte[] first, byte[] second) {
        if (first == second)
            return true;

        if (first == null || second == null)
            return false;

        if (first.length != second.length)
            return false;

        for (int i = 0; i < second.length; i++) {
            if (first[i] != second[i])
                return false;
        }
        return true;
    }
}

Related Tutorials