當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Array mismatch()用法及代碼示例

java.util 包中的 Arrays 類是 Java 集合框架的一部分。此類提供靜態方法來動態創建和訪問 Java 數組。它僅由靜態方法和 Object 類的方法組成。這個類的方法可以被類名本身使用。

mismatch() 是在 Java.util 包的 Arrays 類下定義的方法,用於在 mismatch 方法中作為參數傳遞的兩個數組。此方法返回作為參數傳遞給 mismatch() 函數的兩個數組具有第一個不相等元素的索引。檢查兩個數組是否包含相同的對應元素非常有用。這會在發生不匹配時做出響應。如果兩個數組的對應元素相同,則此函數返回 -1。我們可以通過考慮以下示例來了解其工作原理:

我們有兩個數組,array1 = {2 , 6 , 1 , 10} 和 array2 = {2 , 6 , 11 , 12} 我們想要找到 array1 和 array2 第一個不相等元素的索引。由於前兩個索引具有相同的一組對應元素,因此索引為 2。

借助 mismatch() 方法,我們可以在不遍曆數組的情況下完成上述任務。



用法:

Arrays.mismatch(first_array, second_array);

參數:上述方法接受以下參數:

  • 1. first_array:特定數據類型的數組(第一個數組名)。
  • 2. second_array:同類型的另一個數組(第二個數組名)。

返回值:

1. -1: If both the arrays have same elements at all the corresponding positions.

2. non-negative integer:The index at which both the arrays have first unequal elements.

Note: The data type of both arrays must be the same, and this method follows zero-based indexing. 



範例1:

Java


// Java program to demonstrate the working of
// mismatch() method with arrays of integer type
  
// Including necessary import statements
import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing an integer array
        int array1[] = { 2, 7, 11, 22, 37 };
  
        // Initializing another array
        int array2[] = { 2, 7, 11, 22, 37 };
  
        // Initializing another array
        int array3[] = { 2, 7, 19, 31, 39, 56 };
  
        // Return the first index at which array1
        // array2 have the different element
        int index1 = Arrays.mismatch(array1, array2);
  
        // Return the first index at which array1
        // array3 have the different element
        int index2 = Arrays.mismatch(array1, array3);
  
        // Return the first index at which array2
        // array3 have the different element
        int index3 = Arrays.mismatch(array2, array3);
  
        // Print the first index at which array1
        // array2 have the different element
        System.out.println(
            "The index at which array1 and array2 have first unequal element:"
            + index1);
  
        // Print the first index at which array1
        // array3 have the different element
        System.out.println(
            "The index at which array1 and array3 have first unequal element:"
            + index2);
  
        // Print the first index at which array2
        // array3 have the different element
        System.out.println(
            "The index at which array2 and array3 have first unequal element:"
            + index3);
    }
}
輸出
The index at which array1 and array2 have first unequal element:-1
The index at which array1 and array3 have first unequal element:2
The index at which array2 and array3 have first unequal element:2

範例2:

Java


// Java program to demonstrate the working of
// mismatch() method with arrays of double type
  
// Including necessary import statements
import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing an array containing double values
        double array1[]
            = { 11.21, 22.31, 33.15, 44.18, 55.19, 66.666 };
  
        // Initializing another array
        double array2[]
            = { 11.21, 22.31, 33.15, 44.18, 55.19, 66.666 };
  
        // Initializing another array
        double array3[] = { 11.21, 22, 33, 44, 55, 66 };
  
        // Return the first index at which array1
        // array2 have the different element
        int index1 = Arrays.mismatch(array1, array2);
  
        // Return the first index at which array1
        // array3 have the different element
        int index2 = Arrays.mismatch(array1, array3);
  
        // Return the first index at which array2
        // array3 have the different element
        int index3 = Arrays.mismatch(array2, array3);
  
        // Print the first index at which array1
        // array2 have the different element
        System.out.println(
            "The index at which array1 and array2 have first unequal element:"
            + index1);
  
        // Print the first index at which array1
        // array3 have the different element
        System.out.println(
            "The index at which array1 and array3 have first unequal element:"
            + index2);
  
        // Print the first index at which array2
        // array3 have the different element
        System.out.println(
            "The index at which array2 and array3 have first unequal element:"
            + index3);
    }
}
輸出
The index at which array1 and array2 have first unequal element:-1
The index at which array1 and array3 have first unequal element:1
The index at which array2 and array3 have first unequal element:1

範例3:

Java


// Java program to demonstrate the working of
// mismatch() method with arrays of character type
  
// Including necessary import statements
import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing an character array
        char array1[] = { 'g', 'e', 'e', 'k', 's' };
  
        // Initializing another array
        char array2[] = { 'g', 'e', 'e', 'k', 's' };
  
        // Initializing another array
        char array3[] = { 'g', 'e', 'e', 'k' };
  
        // Return the first index at which array1
        // array2 have the different element
        int index1 = Arrays.mismatch(array1, array2);
  
        // Return the first index at which array1
        // array3 have the different element
        int index2 = Arrays.mismatch(array1, array3);
  
        // Return the first index at which array2
        // array3 have the different element
        int index3 = Arrays.mismatch(array2, array3);
  
        // Print the first index at which array1
        // array2 have the different element
        System.out.println(
            "The index at which array1 and array2 have first unequal element:"
            + index1);
  
        // Print the first index at which array1
        // array3 have the different element
        System.out.println(
            "The index at which array1 and array3 have first unequal element:"
            + index2);
  
        // Print the first index at which array2
        // array3 have the different element
        System.out.println(
            "The index at which array2 and array3 have first unequal element:"
            + index3);
    }
}
輸出
The index at which array1 and array2 have first unequal element:-1
The index at which array1 and array3 have first unequal element:4
The index at which array2 and array3 have first unequal element:4

範例4:

Java


// Java program to demonstrate the working of
// mismatch() method with arrays of boolean type
  
// Including necessary import statements
import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing a boolean array
        boolean array1[] = { true, false, true, false };
  
        // Initializing another array
        boolean array2[] = { true, false, true, false };
  
        // Initializing another array
        boolean array3[] = { true, false, false, true };
  
        // Return the first index at which array1
        // array2 have the different element
        int index1 = Arrays.mismatch(array1, array2);
  
        // Return the first index at which array1
        // array3 have the different element
        int index2 = Arrays.mismatch(array1, array3);
  
        // Return the first index at which array2
        // array3 have the different element
        int index3 = Arrays.mismatch(array2, array3);
  
        // Print the first index at which array1
        // array2 have the different element
        System.out.println(
            "The index at which array1 and array2 have first unequal element:"
            + index1);
  
        // Print the first index at which array1
        // array3 have the different element
        System.out.println(
            "The index at which array1 and array3 have first unequal element:"
            + index2);
  
        // Print the first index at which array2
        // array3 have the different element
        System.out.println(
            "The index at which array2 and array3 have first unequal element:"
            + index3);
    }
}
輸出
The index at which array1 and array2 have first unequal element:-1
The index at which array1 and array3 have first unequal element:2
The index at which array2 and array3 have first unequal element:2



相關用法


注:本文由純淨天空篩選整理自bhuwanesh大神的英文原創作品 Java Array mismatch() Method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。