Método Java Array desajuste() con ejemplos

La clase Arrays en el paquete java.util es parte de Java Collection Framework. Esta clase proporciona métodos estáticos para crear y acceder dinámicamente a arreglos Java. Consiste solo en métodos estáticos y los métodos de la clase Object. Los métodos de esta clase pueden ser utilizados por el propio nombre de la clase.

El desajuste() es un método que se define en la clase Arrays del paquete Java.util y se utiliza con respecto a los dos conjuntos pasados ​​como argumento en el método de desajuste. Este método devuelve el índice en el que dos arrays pasadas como parámetro a la función de desajuste() tienen el primer elemento desigual. Es muy útil comprobar si dos arrays contienen los mismos elementos correspondientes o no. Esto responde cuando se produce una falta de coincidencia. Si ambas arrays tienen los mismos elementos correspondientes, esta función devuelve -1. Podemos entender su funcionamiento considerando el siguiente ejemplo:

Nos dan dos arrays, array1 = {2, 6, 1, 10} y array2 = {2, 6, 11, 12} y queremos encontrar el índice en el que array1 y array2 tienen el primer elemento desigual. Como los dos primeros índices tienen el mismo conjunto de elementos correspondientes, el índice es 2. 

Podemos lograr la tarea anterior sin iterar sobre las arrays con la ayuda del método de desajuste().

Sintaxis:

Arrays.mismatch(first_array, second_array);

Parámetros: El método anterior acepta los siguientes parámetros:

1. first_array: una array (primer nombre de la array) de un tipo de datos particular.

2. second_array: Otra array (segundo nombre de array) del mismo tipo.

Valor de retorno:

1. -1: si ambas arrays tienen los mismos elementos en todas las posiciones correspondientes.

2. entero no negativo: el índice en el que ambas arrays tienen primeros elementos desiguales.

Nota: El tipo de datos de ambas arrays debe ser el mismo y este método sigue la indexación basada en cero. 

Ejemplo 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);
    }
}
Producción

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

Ejemplo 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);
    }
}
Producción

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

Ejemplo 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);
    }
}
Producción

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

Ejemplo 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);
    }
}
Producción

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

Publicación traducida automáticamente

Artículo escrito por bhuwanesh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *