El método compareTo() de la clase java.nio.ShortBuffer se utiliza para comparar un búfer con otro.
Dos búferes cortos 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 cortos se comparan como si se invocara Short.compare(short, short). Un búfer corto no es comparable a ningún otro tipo de objeto.
Sintaxis :
public int compareTo(ShortBuffer that)
Parámetro : este método toma un objeto shortbuffer 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() :
Programa 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 sb int capacity1 = 3; // Creating the ShortBuffer try { // creating object of shortbuffer sb // and allocating size capacity ShortBuffer sb = ShortBuffer.allocate(capacity1); // putting the value in sb sb.put((short)956); sb.put((short)761); sb.put((short)461); // revind the short buffer sb.rewind(); // print the ShortBuffer System.out.println(" ShortBuffer sb: " + Arrays.toString(sb.array())); // creating object of shortbuffer sb1 // and allocating size capacity ShortBuffer sb1 = ShortBuffer.allocate(capacity1); // putting the value in sb1 sb1.put((short)956); sb1.put((short)761); sb1.put((short)461); // revind the short buffer sb1.rewind(); // print the shortBuffer System.out.println(" ShortBuffer sb1: " + Arrays.toString(sb1.array())); // compare both buffer and store the value into integer int i = sb.compareTo(sb1); // 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); } } }
ShortBuffer sb: [956, 761, 461] ShortBuffer sb1: [956, 761, 461] both buffer are lexicographically equal
Programa 2 : cuando este ShortBuffer es mayor que el ShortBuffer 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 sb int capacity1 = 3; // Creating the ShortBuffer try { // creating object of shortbuffer sb // and allocating size capacity ShortBuffer sb = ShortBuffer.allocate(capacity1); // putting the value in sb sb.put((short)956); sb.put((short)761); sb.put((short)41); // revind the short buffer sb.rewind(); // print the ShortBuffer System.out.println(" ShortBuffer s: " + Arrays.toString(sb.array())); // creating object of shortbuffer sb1 // and allocating size capacity ShortBuffer sb1 = ShortBuffer.allocate(capacity1); // putting the value in sb1 sb1.put((short)856); sb1.put((short)761); sb1.put((short)461); // revind the short buffer sb1.rewind(); // print the ShortBuffer System.out.println(" ShortBuffer sb1: " + Arrays.toString(sb1.array())); // compare both buffer and store the value into integer int i = sb.compareTo(sb1); // 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); } } }
ShortBuffer s: [956, 761, 41] ShortBuffer sb1: [856, 761, 461] fb is lexicographically greater than fb1
Programa 3 : cuando este ShortBuffer es menor que el ShortBuffer 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 sb int capacity1 = 3; // Creating the ShortBuffer try { // creating object of shortbuffer sb // and allocating size capacity ShortBuffer sb = ShortBuffer.allocate(capacity1); // putting the value in sb sb.put((short)856); sb.put((short)761); sb.put((short)461); // revind the short buffer sb.rewind(); // print the ShortBuffer System.out.println(" ShortBuffer sb: " + Arrays.toString(sb.array())); // creating object of shortbuffer sb1 // and allocating size capacity ShortBuffer sb1 = ShortBuffer.allocate(capacity1); // putting the value in sb1 sb1.put((short)956); sb1.put((short)761); sb1.put((short)461); // revind the short buffer sb1.rewind(); // print the ShortBuffer System.out.println(" ShortBuffer sb1: " + Arrays.toString(sb1.array())); // compare both buffer and store the value into integer int i = sb.compareTo(sb1); // 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); } } }
ShortBuffer sb: [856, 761, 461] ShortBuffer sb1: [956, 761, 461] fb is lexicographically less than fb1
Publicación traducida automáticamente
Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA