如何比较Java Byte []数组? [英] how to compare the Java Byte[] array?
本文介绍了如何比较Java Byte []数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class ByteArr {
public static void main(String[] args){
Byte[] a = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
Byte[] b = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
byte[] aa = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
byte[] bb = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
System.out.println(a);
System.out.println(b);
System.out.println(a == b);
System.out.println(a.equals(b));
System.out.println(aa);
System.out.println(bb);
System.out.println(aa == bb);
System.out.println(aa.equals(bb));
}
}
我不知道为什么所有这些都打印错误。
I do not know why all of them print false.
当我运行java ByteArray时,答案是false false false false。
When I run "java ByteArray", the answer is "false false false false".
我认为a []等于b [],但JVM告诉我错了,为什么?
I think the a[] equals b[] but the JVM is telling me I am wrong, why??
推荐答案
使用 Arrays.equals()
如果你想比较包含基本类型值(如byte)的数组的实际内容。
Use Arrays.equals()
if you want to compare the actual content of arrays that contain primitive types values (like byte).
System.out.println(Arrays.equals(aa, bb));
使用 Arrays.deepEquals
进行数组比较包含对象。
Use Arrays.deepEquals
for comparison of arrays that contain objects.
这篇关于如何比较Java Byte []数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文