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
相关用法
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Clock withZone()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Array setBoolean()用法及代码示例
- Java FloatBuffer array()用法及代码示例
- Java ByteBuffer array()用法及代码示例
- Java ShortBuffer array()用法及代码示例
- Java DoubleBuffer array()用法及代码示例
- Java Bytes.indexOf(byte[] array, byte target)用法及代码示例
- Java Floats.indexOf(float[] array, float target)用法及代码示例
- Java Doubles.indexOf(double[] array, double target)用法及代码示例
- Java Shorts.indexOf(short[] array, short target)用法及代码示例
- Java Booleans.indexOf(boolean[] array, boolean target)用法及代码示例
- Java Booleans.indexOf(boolean[] array, boolean[] target)用法及代码示例
注:本文由纯净天空筛选整理自bhuwanesh大神的英文原创作品 Java Array mismatch() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。