El método equals() de java.nio.IntBuffer Class se usa para verificar si el búfer dado es igual o no a otro objeto.
Dos buffers int son iguales si, y solo si,
- Tienen el mismo tipo de elemento,
- Tienen el mismo número de elementos restantes, y
- Las dos secuencias de elementos restantes, consideradas independientemente de sus posiciones iniciales, son puntos
iguales.
Este método considera que dos elementos int a y b son iguales si (a == b) || (Int.isNaN(a) && Int.isNaN(b)). Los valores -0 y +0 se consideran iguales, a diferencia de Int.equals(Object).
Un búfer int no es igual a ningún otro tipo de objeto.
Sintaxis:
public boolean equals(Object ob)
Parámetros: este método toma como parámetro el ob , el objeto con el que se va a comparar este búfer.
Valor devuelto: este método devuelve verdadero si, y solo si, este búfer es igual al objeto dado.
A continuación se muestran los ejemplos para ilustrar el método equals() :
Ejemplos 1:
// Java program to demonstrate // equals() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the IntBuffer 1 int capacity1 = 10; // Declaring the capacity of the IntBuffer 2 int capacity2 = 10; // Creating the IntBuffer try { // creating object of Intbuffer 1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // creating object of Intbuffer 2 // and allocating size capacity IntBuffer ib2 = IntBuffer.allocate(capacity2); // putting the value in Intbuffer 1 ib1.put(8); ib1.put(2, 9); ib1.rewind(); // putting the value in Intbuffer 2 ib2.put(8); ib2.put(2, 9); ib2.rewind(); // print the IntBuffer 1 System.out.println(" IntBuffer 1: " + Arrays.toString(ib1.array())); // print the IntBuffer 2 System.out.println(" IntBuffer 2: " + Arrays.toString(ib2.array())); // checking the equality of both IntBuffer boolean ibb = ib1.equals(ib2); // checking if else condition if (ibb) System.out.println("Both are equal"); else System.out.println("Both are not equal"); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } }
IntBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] IntBuffer 2: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] Both are equal
Ejemplos 2:
// Java program to demonstrate // equals() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the IntBuffer 1 int capacity1 = 10; // Declaring the capacity of the IntBuffer 2 int capacity2 = 5; // Creating the IntBuffer try { // creating object of Intbuffer 1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // creating object of Intbuffer 2 // and allocating size capacity IntBuffer ib2 = IntBuffer.allocate(capacity2); // putting the value in Intbuffer 1 ib1.put(8); ib1.put(2, 9); ib1.rewind(); // putting the value in Intbuffer 2 ib2.put(8); ib2.put(2, 9); ib2.rewind(); // print the IntBuffer 1 System.out.println(" IntBuffer 1: " + Arrays.toString(ib1.array())); // print the IntBuffer 2 System.out.println(" IntBuffer 2: " + Arrays.toString(ib2.array())); // checking the equality of both IntBuffer boolean ibb = ib1.equals(ib2); // checking if else condition if (ibb) System.out.println("Both are equal"); else System.out.println("Both are not equal"); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } }
IntBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] IntBuffer 2: [8, 0, 9, 0, 0] Both are not equal
Ejemplos 3:
// Java program to demonstrate // equals() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the IntBuffer 1 int capacity1 = 10; // Declaring the capacity of the IntBuffer 2 int capacity2 = 10; // Creating the IntBuffer try { // creating object of Intbuffer 1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // creating object of Intbuffer 2 // and allocating size capacity IntBuffer ib2 = IntBuffer.allocate(capacity2); // putting the value in Intbuffer 1 ib1.put(8); ib1.put(2, 9); ib1.rewind(); // putting the value in Intbuffer 2 ib2.put(8); ib2.put(2, 9); ib2.put(3, 7); ib2.put(4, 4); ib2.rewind(); // print the IntBuffer 1 System.out.println(" IntBuffer 1: " + Arrays.toString(ib1.array())); // print the IntBuffer 2 System.out.println(" IntBuffer 2: " + Arrays.toString(ib2.array())); // checking the equality of both IntBuffer boolean ibb = ib1.equals(ib2); // checking if else condition if (ibb) System.out.println("Both are equal"); else System.out.println("Both are not equal"); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } }
IntBuffer 1: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] IntBuffer 2: [8, 0, 9, 7, 4, 0, 0, 0, 0, 0] Both are not equal
Publicación traducida automáticamente
Artículo escrito por nitin_sharma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA