El método compareTo() de la clase java.nio.FloatBuffer se utiliza para comparar un búfer con otro. Dos búfer flotantes se comparan comparando sus secuencias de elementos restantes lexicográficamente, sin tener en cuenta la posición inicial de cada secuencia dentro de su búfer correspondiente. Los pares de elementos flotantes se comparan como si se invocara Float.compare(float, float), excepto que -0.0 y 0.0 se consideran iguales. Este método considera que Float.NaN es igual a sí mismo y mayor que todos los demás valores flotantes (incluido Float.POSITIVE_INFINITY). Un búfer flotante no es comparable a ningún otro tipo de objeto.
Sintaxis:
public int compareTo(FloatBuffer that)
Parámetro: este método toma un objeto floatbuffer como parámetro con el que se comparará este búfer.
Valor de retorno: este método devuelve un número entero negativo, cero o un número entero positivo , ya que este búfer es menor, igual o mayor que el búfer dado.
A continuación se muestran los ejemplos para ilustrar el método compareTo():
Ejemplos 1: Cuando ambos FloatBuffer son iguales.
// Java program to demonstrate // compareTo() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the fb int capacity1 = 3; // Creating the FloatBuffer try { // creating object of floatbuffer fb // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity1); // putting the value in fb fb.put(9.56F); fb.put(7.61F); fb.put(4.61F); // revind the float buffer fb.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb: " + Arrays.toString(fb.array())); // creating object of floatbuffer fb1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // putting the value in fb1 fb1.put(9.56F); fb1.put(7.61F); fb1.put(4.61F); // revind the float buffer fb1.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb1: " + Arrays.toString(fb1.array())); // compare both buffer and store the value into integer int i = fb.compareTo(fb1); // if else condition if (i == 0) System.out.println("\nboth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nfb is lexicographically greater than fb1"); else System.out.println("\nfb is lexicographically less than fb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
FloatBuffer fb: [9.56, 7.61, 4.61] FloatBuffer fb1: [9.56, 7.61, 4.61] both buffer are lexicographically equal
Ejemplos 2: cuando este FloatBuffer es mayor que el FloatBuffer pasado
// Java program to demonstrate // compareTo() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the fb int capacity1 = 3; // Creating the FloatBuffer try { // creating object of floatbuffer fb // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity1); // putting the value in fb fb.put(9.56F); fb.put(7.61F); fb.put(4.61F); // revind the float buffer fb.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb: " + Arrays.toString(fb.array())); // creating object of floatbuffer fb1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // putting the value in fb1 fb1.put(8.56F); fb1.put(7.61F); fb1.put(4.61F); // revind the float buffer fb1.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb1: " + Arrays.toString(fb1.array())); // compare both buffer and store the value into integer int i = fb.compareTo(fb1); // if else condition if (i == 0) System.out.println("\nboth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nfb is lexicographically greater than fb1"); else System.out.println("\nfb is lexicographically less than fb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
FloatBuffer fb: [9.56, 7.61, 4.61] FloatBuffer fb1: [8.56, 7.61, 4.61] fb is lexicographically greater than fb1
Ejemplos 3: cuando este FloatBuffer es menor que el FloatBuffer pasado
// Java program to demonstrate // compareTo() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the fb int capacity1 = 3; // Creating the FloatBuffer try { // creating object of floatbuffer fb // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity1); // putting the value in fb fb.put(8.56F); fb.put(7.61F); fb.put(4.61F); // revind the float buffer fb.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb: " + Arrays.toString(fb.array())); // creating object of floatbuffer fb1 // and allocating size capacity FloatBuffer fb1 = FloatBuffer.allocate(capacity1); // putting the value in fb1 fb1.put(9.56F); fb1.put(7.61F); fb1.put(4.61F); // revind the float buffer fb1.rewind(); // print the FloatBuffer System.out.println("FloatBuffer fb1: " + Arrays.toString(fb1.array())); // compare both buffer and store the value into integer int i = fb.compareTo(fb1); // if else condition if (i == 0) System.out.println("\nboth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nfb is lexicographically greater than fb1"); else System.out.println("\nfb is lexicographically less than fb1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
FloatBuffer fb: [8.56, 7.61, 4.61] FloatBuffer fb1: [9.56, 7.61, 4.61] fb is lexicographically less than fb1
Publicación traducida automáticamente
Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA