import
java.util.Arrays;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
class
ArraysEquals
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
int[] q = new int[]...{1,2,3};
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
int[] p = new int[]...{1,2,3};
boolean equals1 = equals1(q,p);
boolean equals2 = equals2(q,p);
System.out.println("Arrays.equals(q,p)的结果:" + equals1);
System.out.println("q.equals(p)的结果:" + equals2);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
static boolean equals1(int[] q,int[] p)...{
if(Arrays.equals(q,p))
return true;
else
return false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
static boolean equals2(int[] q,int[] p)...{
if(q.equals(p))
return true;
else
return false;
}
}
运行结果:
Arrays.equals(q,p)的结果:true
q.equals(p)的结果:false
结论:
Arrays.equals(q,p)是比较两个arrays是否有相同个元素,每个元素是否相等。
q.equals(p)等同于q == p。