"Go" byte[] Array comparison (content comparison)--good

Source: Internet
Author: User
Tags array to string

Original URL: http://amanda.blog.51cto.com/7238262/1215118

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://amanda.blog.51cto.com/7238262/1215118

Two comparisons between arrays (compares the contents of two arrays):

Method One:

Directly call the Java API for comparison

1: Convert byte[] Array to String (premise: byte[] array B1 b2 not empty)

2: Call Equals () method to compare, return true for equality, otherwise return false

1:

String S1 = arrays.tostring (byte[] B1);

String s2 = arrays.tostring (byte[] B2);

2:

S1.equals (S2)

Method Two:

1: Make sure two arrays are not empty, otherwise, return false

2: Compare length first, return False if length is not equal

3: Under the premise of equal length, take out the array subscript content separately, loop compare, return False when different, and exit

public static Boolean Comperebytearray (Byte[] B1, byte[] b2) {

1:
if (B1.length = = 0 | | b2.length = = 0) {
return false;
}

2:
if (b1.length! = b2.length) {
return false;
}

3:
Boolean isequal = true;

for (int i = 0; i < b1.length && i < b2.length; i++) {
if (b1[i]! = B2[i]) {
System.out.println ("different");
IsEqual = false;
Break
}
}
return isequal;
}

This article is from the "Intercept string" blog, so be sure to keep this source http://amanda.blog.51cto.com/7238262/1215118

"Go" byte[] Array comparison (content comparison)--good

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.