El método equals() de java.nio.FloatBuffer Class se usa para verificar si el búfer dado es igual o no a otro objeto.
Dos buffers flotantes 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 flotantes a y b son iguales si (a == b) || (Flotante.esNaN(a) && Flotante.isNaN(b)). Los valores -0.0 y +0.0 se consideran iguales, a diferencia de Float.equals(Object).
Un búfer flotante no es igual a ningún otro tipo de objeto.
Sintaxis:
public boolean equals(Object ob)
Parámetros: este método toma el ob (el objeto con el que se va a comparar este búfer) como parámetro.
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 FloatBuffer 1 int capacity1 = 10; // Declaring the capacity of the FloatBuffer 2 int capacity2 = 10; // Creating the FloatBuffer try { // creating object of floatbuffer 1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // creating object of floatbuffer 2 // and allocating size capacity FloatBuffer fb2 = FloatBuffer.allocate(capacity2); // putting the value in floatbuffer 1 fb1.put(8.56F); fb1.put(2, 9.61F); fb1.rewind(); // putting the value in floatbuffer 2 fb2.put(8.56F); fb2.put(2, 9.61F); fb2.rewind(); // print the FloatBuffer 1 System.out.println(" FloatBuffer 1: " + Arrays.toString(fb1.array())); // print the FloatBuffer 2 System.out.println(" FloatBuffer 2: " + Arrays.toString(fb2.array())); // checking the equality of both FloatBuffer boolean fbb = fb1.equals(fb2); // checking if else condition if (fbb) 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"); } } }
FloatBuffer 1: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] FloatBuffer 2: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.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 FloatBuffer 1 int capacity1 = 10; // Declaring the capacity of the FloatBuffer 2 int capacity2 = 5; // Creating the FloatBuffer try { // creating object of floatbuffer 1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // creating object of floatbuffer 2 // and allocating size capacity FloatBuffer fb2 = FloatBuffer.allocate(capacity2); // putting the value in floatbuffer 1 fb1.put(8.56F); fb1.put(2, 9.61F); fb1.rewind(); // putting the value in floatbuffer 2 fb2.put(8.56F); fb2.put(2, 9.61F); fb2.rewind(); // print the FloatBuffer 1 System.out.println(" FloatBuffer 1: " + Arrays.toString(fb1.array())); // print the FloatBuffer 2 System.out.println(" FloatBuffer 2: " + Arrays.toString(fb2.array())); // checking the equality of both FloatBuffer boolean fbb = fb1.equals(fb2); // checking if else condition if (fbb) 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"); } } }
FloatBuffer 1: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] FloatBuffer 2: [8.56, 0.0, 9.61, 0.0, 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 FloatBuffer 1 int capacity1 = 10; // Declaring the capacity of the FloatBuffer 2 int capacity2 = 10; // Creating the FloatBuffer try { // creating object of floatbuffer 1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // creating object of floatbuffer 2 // and allocating size capacity FloatBuffer fb2 = FloatBuffer.allocate(capacity2); // putting the value in floatbuffer 1 fb1.put(8.56F); fb1.put(2, 9.61F); fb1.rewind(); // putting the value in floatbuffer 2 fb2.put(8.56F); fb2.put(2, 9.61F); fb2.put(3, 7.861F); fb2.put(4, 4.31F); fb2.rewind(); // print the FloatBuffer 1 System.out.println(" FloatBuffer 1: " + Arrays.toString(fb1.array())); // print the FloatBuffer 2 System.out.println(" FloatBuffer 2: " + Arrays.toString(fb2.array())); // checking the equality of both FloatBuffer boolean fbb = fb1.equals(fb2); // checking if else condition if (fbb) 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"); } } }
FloatBuffer 1: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] FloatBuffer 2: [8.56, 0.0, 9.61, 7.861, 4.31, 0.0, 0.0, 0.0, 0.0, 0.0] both are not equal
Publicación traducida automáticamente
Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA