El método compareTo() de la clase java.nio.IntBuffer se utiliza para comparar un búfer con otro. Dos búferes int 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 int se comparan como si se invocara Int.compare(int, int), excepto que -0 y 0 se consideran iguales. Este método considera que Int.NaN es igual a sí mismo y mayor que todos los demás valores int (incluido Int.POSITIVE_INFINITY). Un búfer int no es comparable a ningún otro tipo de objeto.
Sintaxis:
public int compareTo(IntBuffer that)
Parámetro : este método toma un objeto intbuffer 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.
El siguiente programa ilustra el método compareTo() :
Ejemplos 1: Cuando ambos IntBuffer 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 ib int capacity1 = 3; // Creating the IntBuffer try { // creating object of Intbuffer ib // and allocating size capacity IntBuffer ib = IntBuffer.allocate(capacity1); // putting the value in ib ib.put(9); ib.put(7); ib.put(4); // revind the Int buffer ib.rewind(); // print the IntBuffer System.out.println("IntBuffer ib: " + Arrays.toString(ib.array())); // creating object of Intbuffer ib1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // putting the value in ib1 ib1.put(9); ib1.put(7); ib1.put(4); // revind the Int buffer ib1.rewind(); // print the IntBuffer System.out.println("IntBuffer ib1: " + Arrays.toString(ib1.array())); // compare both buffer and store the value into integer int i = ib.compareTo(ib1); // if else condition if (i == 0) System.out.println("\nBoth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nib is lexicographically greater than ib1"); else System.out.println("\nib is lexicographically less than ib1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
IntBuffer ib: [9, 7, 4] IntBuffer ib1: [9, 7, 4] Both buffer are lexicographically equal
Ejemplos 2: cuando este IntBuffer es mayor que el IntBuffer 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 ib int capacity1 = 3; // Creating the IntBuffer try { // creating object of Intbuffer ib // and allocating size capacity IntBuffer ib = IntBuffer.allocate(capacity1); // putting the value in ib ib.put(9); ib.put(7); ib.put(4); // revind the Int buffer ib.rewind(); // print the IntBuffer System.out.println("IntBuffer ib: " + Arrays.toString(ib.array())); // creating object of Intbuffer ib1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // putting the value in ib1 ib1.put(8); ib1.put(7); ib1.put(4); // revind the Int buffer ib1.rewind(); // print the IntBuffer System.out.println("IntBuffer ib1: " + Arrays.toString(ib1.array())); // compare both buffer and store the value into integer int i = ib.compareTo(ib1); // if else condition if (i == 0) System.out.println("\nBoth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nib is lexicographically greater than ib1"); else System.out.println("\nib is lexicographically less than ib1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
IntBuffer ib: [9, 7, 4] IntBuffer ib1: [8, 7, 4] ib is lexicographically greater than ib1
Ejemplo 3: cuando este IntBuffer es menor que el IntBuffer 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 ib int capacity1 = 3; // Creating the IntBuffer try { // creating object of Intbuffer ib // and allocating size capacity IntBuffer ib = IntBuffer.allocate(capacity1); // putting the value in ib ib.put(8); ib.put(7); ib.put(4); // revind the Int buffer ib.rewind(); // print the IntBuffer System.out.println("IntBuffer ib: " + Arrays.toString(ib.array())); // creating object of Intbuffer ib1 // and allocating size capacity IntBuffer ib1 = IntBuffer.allocate(capacity1); // putting the value in ib1 ib1.put(9); ib1.put(7); ib1.put(4); // revind the Int buffer ib1.rewind(); // print the IntBuffer System.out.println("IntBuffer ib1: " + Arrays.toString(ib1.array())); // compare both buffer and store the value into integer int i = ib.compareTo(ib1); // if else condition if (i == 0) System.out.println("\nBoth buffer are lexicographically equal"); else if (i >= 0) System.out.println("\nib is lexicographically greater than ib1"); else System.out.println("\nib is lexicographically less than ib1"); } catch (IllegalArgumentException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
IntBuffer ib: [8, 7, 4] IntBuffer ib1: [9, 7, 4] ib is lexicographically less than ib1
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