• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

判断两个数组是否相等

武飞扬头像
無名.
帮助1

在判断两个数组是否相等之前,我们应该弄清楚数组怎样才算相等,官方给的解释是这样的:

Returns true if the two specified arrays of ints are equal to one another. 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. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

如果指定的两个Int数组彼此相等,则返回true。如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对都相等,则认为两个数组相等。换句话说,如果两个数组以相同的顺序包含相同的元素,则它们是相等的。此外,如果两个数组引用都为null,则认为这两个数组引用相等。

(1)判断两个数组是否相等的方法

  1.  
    public static boolean isSamAarry(int[] arrayA, int[] arrayB) {
  2.  
    if (arrayA == arrayB) {
  3.  
    return true;
  4.  
    }
  5.  
    if (arrayA == null || arrayB == null) {
  6.  
    return false;
  7.  
    }
  8.  
    int len = arrayA.length;
  9.  
    if (len != arrayB.length) {
  10.  
    return false;
  11.  
    }
  12.  
    for (int i = 0; i < len; i ) {
  13.  
    if (arrayA[i] != arrayB[i]) {
  14.  
    return false;
  15.  
    }
  16.  
    }
  17.  
    return true;
  18.  
    }
学新通

(2)在main方法中定义几个数组,并进行测试:

  1.  
    public static void main(String[] args) {
  2.  
    //首先定义几个数组,用于测试
  3.  
    int[] arr1 = null;
  4.  
    int[] arr2 = null;
  5.  
    int[] arr3 = {1, 2, 3, 4, 5};
  6.  
    int[] arr4 = {1, 2, 3, 4, 5};
  7.  
    int[] arr5 = {1, 2, 6, 6, 5};
  8.  
     
  9.  
    //两个数组引用都为null-->相等
  10.  
    System.out.println(isSamAarry(arr1,arr2)); //true
  11.  
    //两个数组引用其中一个为null-->不等
  12.  
    System.out.println(isSamAarry(arr1,arr3)); //false
  13.  
    //相同顺序的相同元素--->相等
  14.  
    System.out.println(isSamAarry(arr3,arr4)); //true
  15.  
    System.out.println(isSamAarry(arr4,arr5)); //false
  16.  
    }
学新通

以上为手写的方法,并进行了调用。其实在Java中已经有写好的相关方法(Arrays类提供了equals()方法比较整个数组),直接调用即可:

  1.  
    public static void main(String[] args) {
  2.  
    int[] arr1 = null;
  3.  
    int[] arr2 = null;
  4.  
    int[] arr3 = {1, 2, 3, 4, 5};
  5.  
    int[] arr4 = {1, 2, 3, 4, 5};
  6.  
    int[] arr5 = {1, 2, 6, 6, 5};
  7.  
     
  8.  
    System.out.println(Arrays.equals(arr1,arr2)); //true
  9.  
    System.out.println(Arrays.equals(arr1,arr3)); //false
  10.  
    System.out.println(Arrays.equals(arr3,arr4)); //true
  11.  
    System.out.println(Arrays.equals(arr4,arr5)); //false
  12.  
    }

源码如下:

  1.  
    public static boolean equals(int[] a, int[] a2) {
  2.  
    if (a==a2)
  3.  
    return true;
  4.  
    if (a==null || a2==null)
  5.  
    return false;
  6.  
     
  7.  
    int length = a.length;
  8.  
    if (a2.length != length)
  9.  
    return false;
  10.  
     
  11.  
    for (int i=0; i<length; i )
  12.  
    if (a[i] != a2[i])
  13.  
    return false;
  14.  
     
  15.  
    return true;
  16.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgekbia
系列文章
更多 icon
同类精品
更多 icon
继续加载