檢查兩個數組是否相等或包含不匹配項

使用本教程可以確定使用JDK API兩個數組相等還是不匹配。

抽絲剝繭 細說架構那些事——【優銳課】


檢查兩個數組是否相等

通過Arrays.equals()方法可以很容易地檢查兩個數組是否相等。對於原始類型,對象和泛型,此標誌方法具有多種形式。它還支持比較器。讓我們考慮以下三個整數數組:

1

int[] integers1 = {3, 4, 5, 6, 1, 5};


2

int[] integers2 = {3, 4, 5, 6, 1, 5};


3

int[] integers3 = {3, 4, 5, 6, 1, 3};


現在,讓我們檢查integers1是否等於integers2,以及integers1是否等於integers3。這很簡單:

 

1

boolean i12 = Arrays.equals(integers1, integers2); // true

2

boolean i13 = Arrays.equals(integers1, integers3); // false

 

 


前面的示例檢查兩個數組是否相等,但是我們可以通過以下方法檢查兩個數組(或範圍)是否相等:

1

boolean equals(int[] a, int aFromIndex,

2

               int aToIndex, int[] b, int bFromIndex, int bToIndex)

 

 


因此,我們通過範圍[aFromIndexaToIndex)劃分第一個數組的段,並通過範圍[bFromIndexbToIndex)劃分第二個數組的段:

1

// true

2

boolean is13 = Arrays.equals(integers1, 1, 4, integers3, 1, 4);

 

 


現在,讓我們假設三個Melon數組:

 

1

public class Melon {

2

  

3

  private final String type;

4

  private final int weight;

5

  

6

  public Melon(String type, int weight) {

7

    this.type = type;

8

    this.weight = weight;

9

  }

10

    

11

  @Override

12

    public int hashCode() {

13

        int hash = 7;

14

        hash = 83 * hash + Objects.hashCode(this.type);

15

        hash = 83 * hash + this.weight;

16

        return hash;

17

    }

18

    

19

  @Override

20

  public boolean equals(Object obj) {

21

      

22

      if (this == obj) {

23

          return true;

24

      }

25

      

26

      if (obj == null) {

27

         return false;

28

      }

29

      

30

      if (getClass() != obj.getClass()) {

31

          return false;

32

      }

33

      

34

      final Melon other = (Melon) obj;

35

      if (this.weight != other.weight) {

36

          return false;

37

      }

38

      

39

      if (!Objects.equals(this.type, other.type)) {

40

          return false;

41

      }

42

      

43

      return true;

44

  }

45

}

46

    

47

Melon[] melons1 = {

48

  new Melon("Horned", 1500), new Melon("Gac", 1000)

49

};

50

  

51

Melon[] melons2 = {

52

  new Melon("Horned", 1500), new Melon("Gac", 1000)

53

};

54

  

55

Melon[] melons3 = {

56

  new Melon("Hami", 1500), new Melon("Gac", 1000)

57

};

 

 


根據equals()約定或基於指定的Comparator,將兩個對象數組視爲相等。我們可以輕鬆地檢查melons1是否等於melons2,以及melons1是否等於melons3,如下所示:

1

boolean m12 = Arrays.equals(melons1, melons2); // true

2

boolean m13 = Arrays.equals(melons1, melons3); // false

 

 


並且,在明確的範圍內,我們可以使用以下方法:

1

 boolean equals(Object[] a, int aFromIndex,

2

                int aToIndex, Object[] b, int bFromIndex, int bToIndex)

 

 


例如:

1

boolean ms13 = Arrays.equals(melons1, 1, 2, melons3, 1, 2); // false

 

 


儘管這些示例依賴於Melon.equals()實現,但是以下兩個示例依賴於以下兩個Comparator

 

1

Comparator<Melon> byType = Comparator.comparing(Melon::getType);

2

Comparator<Melon> byWeight = Comparator.comparing(Melon::getWeight);

 

 


使用boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) ,我們具有以下內容:

1

boolean mw13 = Arrays.equals(melons1, melons3, byWeight); // true

2

boolean mt13 = Arrays.equals(melons1, melons3, byType); // false

 

 


並且,在明確的範圍內,使用:

1

Comparator, <T> boolean equals(T[] a, int aFromIndex, int aToIndex, T[] b,

2

                               int bFromIndex, int bToIndex, Comparator<? super T> cmp)

 

 


我們有以下內容:

1

// true

2

boolean mrt13 = Arrays.equals(melons1, 1, 2, melons3, 1, 2, byType);

 

 


檢查兩個數組是否包含不匹配項

如果兩個數組相等,則不匹配應返回-1。但是,如果兩個數組不相等,則不匹配應返回兩個給定數組之間第一個不匹配的索引。爲了解決此問題,我們可以依靠JDK 9 Arrays.mismatch()方法。

例如,我們可以檢查integers1integers2之間的不匹配,如下所示:

1

int mi12 = Arrays.mismatch(integers1, integers2); // -1

 

 


結果爲-1,因爲integers1integers2相等。但是,如果我們檢查integers1integers3,則會收到值5,這是這兩者之間第一個不匹配的索引:

 

1

int mi13 = Arrays.mismatch(integers1, integers3); // 5

 

 

如果給定的數組具有不同的長度,而較小的數組是較大數組的前綴,則返回的不匹配項是較小數組的長度。


對於Object數組,還有專用的mismatch()方法。這些方法依靠equals()合約或給定的Comparator。我們可以檢查melons1melons2之間是否不匹配,如下所示:

1

int mm12 = Arrays.mismatch(melons1, melons2); // -1

 

 


如果在第一個索引上發生不匹配,則返回值爲0。這在melons1melons3的情況下發生:

1

int mm13 = Arrays.mismatch(melons1, melons3); // 0

 

 


Arrays.equals()一樣,我們可以使用Comparator在明確範圍內檢查不匹配情況:

 

1

// range [1, 2), return -1

2

int mms13 = Arrays.mismatch(melons1, 1, 2, melons3, 1, 2);

3

 

4

// Comparator by melon's weights, return -1

5

int mmw13 = Arrays.mismatch(melons1, melons3, byWeight);

6

  

7

// Comparator by melon's types, return 0

8

int mmt13 = Arrays.mismatch(melons1, melons3, byType);

9

  

10

// range [1,2) and Comparator by melon's types, return -1

11

int mmrt13 = Arrays.mismatch(melons1, 1, 2, melons3, 1, 2, byType);

 

 

OK! 

 感謝閱讀!

另外近期整理了一套完整的Java架構思維導圖,分享給同樣正在認真學習的每位朋友~歡迎私信交流!

課程大綱優銳課水印簡版_副本_副本.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章