📜  Java Array mismatch() 方法及示例

📅  最后修改于: 2022-05-13 01:55:14.985000             🧑  作者: Mango

Java Array mismatch() 方法及示例

Java.util中的数组 包是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:

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);
    }
}


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);
    }
}


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);
    }
}


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

示例 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