集册 Java实例教程 如果给定的int数组具有相同的长度并以相同的顺序包含相同的值,则返回

如果给定的int数组具有相同的长度并以相同的顺序包含相同的值,则返回

欢马劈雪     最近更新时间:2020-01-02 10:19:05

360
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
如果给定的int数组具有相同的长度并以相同的顺序包含相同的值,则返回
/**来自 
 N o w  J a v a  .   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.nowjava;


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)
        /* from 
        时代Java公众号 - nowjava.com*/

            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;


        
展开阅读全文