java 数组 强制转换_在Java中将对象强制转换为特定数组

有一个很好的等于实用程序

boolean org.apache.commons.lang.ArrayUtils.isEquals(Object array1, Object array2)它将运行这样的东西 - 当然你不想手动编写:-)

public static boolean isEquals(Object array1, Object array2) {

return new EqualsBuilder().append(array1, array2).isEquals();

}

public EqualsBuilder append(Object lhs, Object rhs) {

if (isEquals == false) {

return this;

}

if (lhs == rhs) {

return this;

}

if (lhs == null || rhs == null) {

this.setEquals(false);

return this;

}

Class lhsClass = lhs.getClass();

if (!lhsClass.isArray()) {

// The simple case, not an array, just test the element

isEquals = lhs.equals(rhs);

} else if (lhs.getClass() != rhs.getClass()) {

// Here when we compare different dimensions, for example: a boolean[][] to a boolean[]

this.setEquals(false);

}

// 'Switch' on type of array, to dispatch to the correct handler

// This handles multi dimensional arrays of the same depth

else if (lhs instanceof long[]) {

append((long[]) lhs, (long[]) rhs);

} else if (lhs instanceof int[]) {

append((int[]) lhs, (int[]) rhs);

} else if (lhs instanceof short[]) {

append((short[]) lhs, (short[]) rhs);

} else if (lhs instanceof char[]) {

append((char[]) lhs, (char[]) rhs);

} else if (lhs instanceof byte[]) {

append((byte[]) lhs, (byte[]) rhs);

} else if (lhs instanceof double[]) {

append((double[]) lhs, (double[]) rhs);

} else if (lhs instanceof float[]) {

append((float[]) lhs, (float[]) rhs);

} else if (lhs instanceof boolean[]) {

append((boolean[]) lhs, (boolean[]) rhs);

} else {

// Not an array of primitives

append((Object[]) lhs, (Object[]) rhs);

}

return this;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值