Java Arrays mismatch(long[] a, long[] b) Method



Description

The Java Arrays mismatch(long[] a, long[] b) method finds and returns the first mismatch between two long arrays. In case of no mismatch, -1 is returned. In case of common prefix, length of the common index is returned. In case one array is a proper prefix of other array, smaller array's length is returned.

Declaration

Following is the declaration for java.util.Arrays.mismatch(long[] a, long[] b) method

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

Parameters

  • a − This is the first array to be tested for mismatch.

  • b − This is the second array to be tested for mismatch.

Return Value

This method the index of the first mismatch between the two arrays, otherwise -1.

Exception

  • NullPointerException − if either array is null.

Java Arrays mismatch(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex) Method

Description

The Java Arrays mismatch(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex) method finds and returns the first mismatch between two long arrays in the given ranges. In case of any array null, a NullPointerException is thrown.

Declaration

Following is the declaration for java.util.Arrays.mismatch(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex) method

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

Parameters

  • a − This is the first array to be tested for mismatch.

  • aFromIndex − This is the index of the first element (inclusive) of first array to be tested for mismatch.

  • aToIndex − This is the index of the last element (exclusive) of first array to be tested for mismatch.

  • b − This is the second array to be tested for mismatch.

  • bFromIndex − This is the index of the first element (inclusive) of second array to be tested for mismatch.

  • bToIndex − This is the index of the last element (exclusive) of second array tto be tested for mismatch.

Return Value

This method returns the relative index of the first mismatch between the two arrays over the specified ranges, otherwise -1.

Exception

  • IllegalArgumentException − if aFromIndex > aToIndex or if bFromIndex > bToIndex

  • ArrayIndexOutOfBoundsException − if aFromIndex < 0 or aToIndex > a.length or if bFromIndex < 0 or bToIndex > b.length

  • NullPointerException − if either array is null

Checking Arrays of longs for Mismatch Example

The following example shows the usage of Java Arrays mismatch(long[], long[]) method. First, we've created two arrays of same longs, and checked them using mismatch() method. Result is printed.

package com.tutorialspoint;

import java.util.Arrays;

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

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

      // initialize second long array
      long array2[] = { 10L, 20L, 5L, 15L, 25L };
            
      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);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

No mismatch. Arrays are same.

Checking Sub-Arrays of longs for Mismatch Example

The following example shows the usage of Java Arrays mismatch​(long[],int,int,long[],int,int) method. First, we've created two arrays of different longs, and checked their sub-arrays using mismatch() method. Result is printed.

package com.tutorialspoint;

import java.util.Arrays;

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

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

      // initialize second long array
      long array2[] = { 10L, 23L, 5L, 15L, 25L };
      
      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);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

First mismatch is at index: 1

Checking Sub-Arrays of longs for Mismatch Example

The following example shows the usage of Java Arrays mismatch(long[],int,int,long[],int,int) method. First, we've created two arrays of different longs, and checked their sub-arrays using mismatch() method. Result is printed.

package com.tutorialspoint;

import java.util.Arrays;

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

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

      // initialize second long array
      long array2[] = { 10L, 23L, 5L, 15L, 25L };
      
      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);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

No mismatch. Arrays are same.
java_util_arrays.htm
Advertisements