Java 陣列 mismatch(byte[] a, byte[] b) 方法



描述

Java Arrays mismatch(byte[] a, byte[] b) 方法查詢並返回兩個位元組陣列之間的第一個不匹配項。如果沒有任何不匹配,則返回 -1。如果存在公共字首,則返回公共索引的長度。如果一個數組是另一個數組的正確字首,則返回較小陣列的長度。

宣告

以下是java.util.Arrays.mismatch(byte[] a, byte[] b) 方法的宣告

public static int mismatch(byte[] a, byte[] b)

引數

  • a − 這是第一個要測試不匹配的陣列。

  • b − 這是第二個要測試不匹配的陣列。

返回值

此方法返回兩個陣列之間第一個不匹配項的索引,否則返回 -1。

異常

  • NullPointerException − 如果任一陣列為空。

Java Arrays mismatch(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex) 方法

描述

Java Arrays mismatch(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex) 方法查詢並返回在給定範圍內兩個位元組陣列之間的第一個不匹配項。如果任何陣列為空,則丟擲 NullPointerException。

宣告

以下是java.util.Arrays.mismatch(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex) 方法的宣告

public static int mismatch​(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)

引數

  • a − 這是第一個要測試不匹配的陣列。

  • aFromIndex − 這是第一個陣列中第一個要測試不匹配的元素的索引(包含)。

  • aToIndex − 這是第一個陣列中最後一個要測試不匹配的元素的索引(不包含)。

  • b − 這是第二個要測試不匹配的陣列。

  • bFromIndex − 這是第二個陣列中第一個要測試不匹配的元素的索引(包含)。

  • bToIndex − 這是第二個陣列中最後一個要測試不匹配的元素的索引(不包含)。

返回值

此方法返回在指定範圍內兩個陣列之間第一個不匹配項的相對索引,否則返回 -1。

異常

  • IllegalArgumentException − 如果 aFromIndex > aToIndex 或 bFromIndex > bToIndex

  • ArrayIndexOutOfBoundsException − 如果 aFromIndex < 0 或 aToIndex > a.length 或 bFromIndex < 0 或 bToIndex > b.length

  • NullPointerException − 如果任一陣列為空

檢查位元組陣列不匹配的示例

以下示例演示了 Java Arrays mismatch(byte[], byte[]) 方法的用法。首先,我們建立了兩個相同位元組的陣列,並使用 mismatch() 方法檢查它們。結果已列印。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initialize first byte array
      byte array1[] = { 10, 20, 5, 15, 25 };

      // initialize second byte array
      byte array2[] = { 10, 20, 5, 15, 25 };
            
      int result = Arrays.mismatch(array1, array2);
      
      if(result == -1) {
    	  System.out.println("No mismatch. Arrays are same.");
      } else {
    	  System.out.println("First mismatch is at index: " + result);
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

No mismatch. Arrays are same.

檢查位元組子陣列不匹配的示例

以下示例演示了 Java Arrays mismatch(byte[],int,int,byte[],int,int) 方法的用法。首先,我們建立了兩個不同位元組的陣列,並使用 mismatch() 方法檢查它們的子陣列。結果已列印。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initialize first byte array
      byte array1[] = { 10, 20, 5, 15, 25 };

      // initialize second byte array
      byte array2[] = { 10, 23, 5, 15, 25 };
      
      int result = Arrays.mismatch(array1, 0, 2, array2, 0, 2);
      
      if(result == -1) {
    	  System.out.println("No mismatch. Arrays are same.");
      } else {
    	  System.out.println("First mismatch is at index: " + result);
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

First mismatch is at index: 1

檢查位元組子陣列不匹配的示例

以下示例演示了 Java Arrays mismatch(byte[],int,int,byte[],int,int) 方法的用法。首先,我們建立了兩個不同位元組的陣列,並使用 mismatch() 方法檢查它們的子陣列。結果已列印。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initialize first byte array
      byte array1[] = { 10, 20, 5, 15, 25 };

      // initialize second byte array
      byte array2[] = { 10, 23, 5, 15, 25 };
      
      int result = Arrays.mismatch(array1, 2, 5, array2, 2, 5);
      
      if(result == -1) {
    	  System.out.println("No mismatch. Arrays are same.");
      } else {
    	  System.out.println("First mismatch is at index: " + result);
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

No mismatch. Arrays are same.
java_util_arrays.htm
廣告