Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compare the equality of two arrays and detect mismatches in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to compare the equality of two arrays and detect mismatches in Java". The explanation in this article is simple and clear and easy to learn and understand. please follow the editor's train of thought to study and learn "how to compare two arrays in Java and detect mismatches".

Question: "how to compare the equality of two arrays and how to detect mismatches?" Strictly speaking, these are two questions, one is to compare whether the array is equal, and the other is which elements are caused if the array is not equal.

01. Compare whether the array is equal

You can use the Arrays.equals () method to compare whether two arrays are equal, which can be basic data types, reference data types, and generics. Let's take a string as an example.

String [] str1 = {"Shen", "Mo", "Wang", "two"}; String [] str2 = {"Shen", "Mo", "Wang", "two"}; String [] str3 = {"Shen", "Mo", "Wang", "three"}

Now, let's compare whether str1 is equal to str2 and whether str1 is equal to str3. (although you can guess the answer without code comparison, please pretend to cooperate.)

String [] str1 = {"Shen", "Mo", "Wang", "two"}; String [] str2 = {"Shen", "Mo", "Wang", "II"}; String [] str3 = {"Shen", "Mo", "Wang", "three"}; System.out.println (Arrays.equals (str1, str2)); System.out.println (str1, str3))

The result of the program output is as follows:

True false

Not bad, exactly in line with our expectations. In addition, we can determine whether the ranges specified in the two arrays are equal by the following methods:

Boolean equals (Object [] a, int aFromIndex, int aToIndex, Object [] b, int bFromIndex, int bToIndex)

Compare whether the first three elements in str1 and str3 are equal:

System.out.println (Arrays.equals (str1, 0,3, str3, 0,3))

The result of the program output is as follows:

True

Now, let's customize a class Writer that has two fields: age of type int and name of type String, and overrides the equals () and hashCode () methods.

Public class Writer {private int age; private String name; @ Override public boolean equals (Object o) {if (this = = o) return true; if (o = = null | | getClass ()! = o.getClass ()) return false; Writer writer = (Writer) o; return age = = writer.age & & Objects.equals (name, writer.name) } @ Override public int hashCode () {return Objects.hash (age, name);} public Writer (int age, String name) {this.age = age; this.name = name;} / / getter/setter}

To create three arrays of type Writer:

Writer [] writer1 = {new Writer (18, Silent King 2), new Writer (16, Silent King 3)}; Writer [] writer2 = {new Writer (18, Silent King 2), new Writer (16, Silent King 3)}; Writer [] writer3 = {new Writer (18, Silent King 1), new Writer (16, Silent King 3)}

Let's compare whether writer1 is equal to writer2 and whether writer1 is equal to writer3.

System.out.println (Arrays.equals (writer1,writer2)); System.out.println (Arrays.equals (writer1,writer3))

The result of the program output is as follows:

True false

The answer is exactly what we expected, because we rewrote the equals () method-- if age is equal and name is equal, that means two Writer objects are equal. If we hadn't overridden the method, we could use the Comparator comparator at this point.

Comparator byAge = Comparator.comparing (Writer::getAge); Comparator byName = Comparator.comparing (Writer::getName)

ByAge is compared through Writer's age, and byName is compared through Writer's name. Then let's compare writer1 and writer3 through the comparators byAge and byName.

System.out.println (Arrays.equals (writer1, writer3, byAge)); System.out.println (Arrays.equals (writer1, writer3, byName))

The result of the program output is as follows:

True false

The answer is exactly as expected: age (18 and 16) in writer1 array is exactly the same as age (18 and 16) in writer3 array; name (Silent King 2 and Silent King 3) in writer1 array is not exactly the same as name (Silent King 1 and Silent King 3) in writer3 array.

02. Mismatch detected

You can use the Arrays.mismatch () method to find out which elements in the two arrays are not equal. If the two arrays are exactly equal, the method returns-1; otherwise, it returns the subscript of the first mismatch.

Let's first see if str1 and str2 have unequal elements.

System.out.println (Arrays.mismatch (str1, str2))

The result of the program output is as follows:

-1

It is consistent with our expected results, because there is no mismatch between str1 and str2. Let's take a look at str1 and str3 again.

System.out.println (Arrays.mismatch (str1, str3))

The result of the program output is as follows:

three

It is true that the mismatch starts with the element with subscript 3, because the element with subscript 3 in str1 is "two" and the element with subscript 3 in str3 is "three".

The Arrays.mismatch () method also applies to the custom type Writer.

System.out.println (Arrays.mismatch (writer1,writer2)); System.out.println (Arrays.mismatch (writer1,writer3))

The result of the program output is as follows:

-1 0

The results are consistent with our expectations, because there is no mismatch between writer1 and writer2, and the elements that are not equal to writer1 and writer3 start at 1, with an index of 0.

You can also use Comparator to detect unequal elements:

System.out.println (Arrays.mismatch (writer1, writer3, byAge)); System.out.println (Arrays.mismatch (writer1, writer3, byName))

The result of the program output is as follows:

-10 Thank you for your reading, the above is the content of "how to compare the equality of two arrays and detect mismatches in Java". After the study of this article, I believe you have a deeper understanding of how to compare the equality of two arrays and detect mismatches in Java. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report