Java的数组比较 [英] Java comparing Arrays

查看:112
本文介绍了Java的数组比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有未知类型的两个数组...有没有办法来检查的元素都是相同的:

I have two Arrays of unknown type...is there a way to check the elements are the same:

public static boolean equals(Object a , Object b) {
  if (a instanceof int[])
    return Arrays.equals((int[]) a, (int[])b);
  if (a instanceof double[]){
    ////etc
}

我想这样做没有所有的instanceof检查....

I want to do this without all the instanceof checks....

推荐答案

<一个href=\"http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/ArrayUtils.html\">ArrayUtils.isEquals()从 Apache的百科全书正是这么做的。它还可以处理多维数组。

ArrayUtils.isEquals() from Apache Commons does exactly that. It also handles multi-dimensional arrays.

这篇关于Java的数组比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆