Java中数组之间的比较
类别: JAVA教程
// null 元素是相等的.
boolean[] bArr1 = null;
boolean[] bArr2 = null;
boolean b = Arrays.equals(bArr1, bArr2); // true
// 比较两个boolean数组.
bArr1 = new boolean[]{true, false};
bArr2 = new boolean[]{true, false};
b = Arrays.equals(bArr1, null); // false
b = Arrays.equals(bArr1, bArr2); // true
// 对于原生类型的数组可以用equals().
b = Arrays.equals(new byte[]{0}, new byte[]{0}); // true
b = Arrays.equals(new char[]{\'a\'}, new char[]{\'a\'}); // true
b = Arrays.equals(new short[]{0}, new short[]{0}); // true
b = Arrays.equals(new int[]{0}, new int[]{0}); // true
b = Arrays.equals(new long[]{0L}, new long[]{0L}); // true
b = Arrays.equals(new float[]{0F}, new float[]{0F}); // true
b = Arrays.equals(new double[]{0D}, new double[]{0D}); // true
// 如果比较的是Object型的数组,null元素是相等的.
// 如果于元素不是null, Java虚拟机会自动调用Object.equals().
b = Arrays.equals(new String[]{"a"}, new String[]{"a"}); // true
b = Arrays.equals(new String[]{null}, new String[]{null}); // true
b = Arrays.equals(new String[]{"a"}, new String[]{null}); // false
boolean[] bArr1 = null;
boolean[] bArr2 = null;
boolean b = Arrays.equals(bArr1, bArr2); // true
// 比较两个boolean数组.
bArr1 = new boolean[]{true, false};
bArr2 = new boolean[]{true, false};
b = Arrays.equals(bArr1, null); // false
b = Arrays.equals(bArr1, bArr2); // true
// 对于原生类型的数组可以用equals().
b = Arrays.equals(new byte[]{0}, new byte[]{0}); // true
b = Arrays.equals(new char[]{\'a\'}, new char[]{\'a\'}); // true
b = Arrays.equals(new short[]{0}, new short[]{0}); // true
b = Arrays.equals(new int[]{0}, new int[]{0}); // true
b = Arrays.equals(new long[]{0L}, new long[]{0L}); // true
b = Arrays.equals(new float[]{0F}, new float[]{0F}); // true
b = Arrays.equals(new double[]{0D}, new double[]{0D}); // true
// 如果比较的是Object型的数组,null元素是相等的.
// 如果于元素不是null, Java虚拟机会自动调用Object.equals().
b = Arrays.equals(new String[]{"a"}, new String[]{"a"}); // true
b = Arrays.equals(new String[]{null}, new String[]{null}); // true
b = Arrays.equals(new String[]{"a"}, new String[]{null}); // false
- 上一篇: java小游戏-贪吃蛇
- 下一篇: 从XML到Java代码的数据绑定之对象
-= 资 源 教 程 =-
文 章 搜 索