我们知道判断字符串相等使用的是equals方法,那么要是判断两个字符串数组是否相等呢,要是char数组呢,同样的java.util.Arrays类提供了equals()方法,如下是官方API:

/**
     * Returns <tt>true</tt> if the two specified arrays of Objects are
     * <i>equal</i> to one another.  The two arrays are considered equal if
     * both arrays contain the same number of elements, and all corresponding
     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
     * they contain the same elements in the same order.  Also, two array
     * references are considered equal if both are <tt>null</tt>.<p>
     *
     * @param a one array to be tested for equality
     * @param a2 the other array to be tested for equality
     * @return <tt>true</tt> if the two arrays are equal
     */
    public static boolean equals(Object[] a, Object[] a2) {
        if (a==a2)
            return true;
        if (a==null || a2==null)
            return false;

int length = a.length;
        if (a2.length != length)
            return false;

for (int i=0; i<length; i++) {
            Object o1 = a[i];
            Object o2 = a2[i];
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
        }

return true;
    }

看看上面的红色部分,其实底层调用的还是Object 的equals方法

Arrays.equals()的更多相关文章

  1. 数组之间的比较应当用Arrays.equals()

    被坑了,数组之间的比较不能用“==”,应当用Arrays.equals() 如果是原生数组(即数组中的值是几大基本数据类型之一的)之间的比较可以直接用,如果数组中的值不是原生的基本数据类型,那么再使用 ...

  2. java数组、java.lang.String、java.util.Arrays、java.lang.Object的toString()方法和equals()方法详解

    public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...

  3. Arrays类的运用,二分法,数组的复制,命令行参数的运用,二维数组,Object,equals

    /*Arrays jdk中为了便于开发,给开发者提供了Arrays类, 其中包含了很多数组的常用操作.例如快速输出.排序.查找等.*/ import java.util.Arrays; public ...

  4. Object、String、数组的 toString() 方法和 equals() 方法及java.util.Arrays

    public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...

  5. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  6. Arrays数组的常用方法

    下面代码主要说明了Arrays数组的几个常用方法(红色字体) import java.util.Scanner;import java.util.Arrays;      public class T ...

  7. JAVA源码走读(二)二分查找与Arrays类

    给数组赋值:通过fill方法. 对数组排序:通过sort方法,按升序.比较数组:通过equals方法比较数组中元素值是否相等.查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找 ...

  8. 数组 Arrays类

    1.数组是一组变量集合,用来存储相同数据类型的一组数据的连续的空间. *数组名(标识符)连续空间首地址. *元素下标标明元素在数组中的位置,从0开始. *每个元素都可以通过下标来访问. *数组长度固定 ...

  9. Java学习笔记七——数组工具类Arrays

    数组工具类Arrays Java提供的Arrays类里包含的一些static修饰的方法可以直接操作数组.若将里面的方法用熟的话,那开发效率会大大提高.下面介绍其中的方法. List<T> ...

随机推荐

  1. ajax再接触

    贴三个目前写的已经工作了的ajax,并且给出备注: 1.这是第一个ajax以及它的接口文档,需要注意的是1.因为返回数据是xml所以多加了contentType:"application/x ...

  2. [翻译]了解ASP.NET底层架构(八)

    原文地址:http://www.cnblogs.com/tmfc/archive/2006/09/04/493304.html [翻译]了解ASP.NET底层架构(完) [翻译]了解ASP.NET底层 ...

  3. mouseover,mouseenter,mouseleave,mouseout

    mouseover和mouseout对应 //鼠标移入移出触发该元素及子元素 mouseenter和mouseleave对应 //鼠标移入移出只触发该元素 看完例子即可知道其区别: mouseover ...

  4. HDU-----(1083)Courses(最大匹配)

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  5. Multiply Strings [LeetCode]

    Problem Description http://oj.leetcode.com/problems/multiply-strings/ Basic idea is to multiply two ...

  6. 自定义头文件 No such file or directory

    my_file.h为你的头文件名 要用#include"my_file.h",而不能是#include<my_file.h>. 如果头文件名在尖括号<>里, ...

  7. Chrome plug-in 和Extension

    "扩展"和"插件",其实都是软件组件的一种形式,Chrome 只不过是把两种类型的组件分别给与了专有名称,一个叫"扩展",另一个叫" ...

  8. mysql 5.7 zip 文件在 windows下的安装

    1.下载mysql最新版本. http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.15-winx64.zip 2.解压到文件夹. D:\softwa ...

  9. myeclipse设置编码格式的4种情况

    (1).设置myeclipse工作空间的编码格式,作用范围最大 window-->preference-->general-->workspace-->text file en ...

  10. Servlet+JSP+JavaBean开发模式(MVC)介绍

    好伤心...写登陆注册之前看见一篇很好的博文,没有收藏,然后找不到了. 前几天在知乎上看见一个问题,什么时候感觉最无力. 前两天一直想回答:尝试过google到的所有solve case,结果bug依 ...