Clase de pantalones cortos | guayaba | Java

Shorts es una clase de utilidad para short de tipo primitivo. Proporciona métodos de utilidad estáticos relacionados con primitivas cortas, que aún no se encuentran ni en Short ni en Arrays.

Declaración :

@GwtCompatible(emulated=true)
public final class Shorts
extends Object

La siguiente tabla muestra el resumen de campo para la clase de pantalones cortos de guayaba:


Some of the methods provided by Guava Shorts Class are :

Exceptions :

  • checkedCast: IllegalArgumentException si el valor es mayor que Short.MAX_VALUE o menor que Short.MIN_VALUE
  • min: IllegalArgumentException si la array está vacía.
  • max: IllegalArgumentException si la array está vacía.
  • fromByteArray: IllegalArgumentException si los bytes tienen menos de 2 elementos.
  • sureCapacity: IllegalArgumentException si minLength o padding es negativo.
  • toArray: NullPointerException si la colección o cualquiera de sus elementos es nulo.

La siguiente tabla muestra algunos otros métodos provistos por Guava Shorts Class:

A continuación se dan algunos ejemplos que muestran la implementación de los métodos de Guava Shorts Class:
Ejemplo 1:

// Java code to show implementation
// of Guava Shorts.asList() method
  
import com.google.common.primitives.Shorts;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        short arr[] = { 3, 4, 5, 6, 7 };
  
        // Using Shorts.asList() method which convert
        // array of primitives to array of objects
        List<Short> myList = Shorts.asList(arr);
  
        // Displaying the elements
        System.out.println(myList);
    }
}

Producción :

[3, 4, 5, 6, 7]

Ejemplo 2:

// Java code to show implementation
// of Guava Shorts.indexOf() method
  
import com.google.common.primitives.Shorts;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        short[] arr = { 3, 4, 5, 6, 7 };
  
        // Displaying the index for
        // first occurrence of given target
        System.out.println(Shorts.indexOf(arr, (short)5));
    }
}

Producción :

2

Ejemplo 3:

// Java code to show implementation
// of Guava Shorts.concat() method
  
import com.google.common.primitives.Shorts;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        short[] arr1 = { 3, 4, 5 };
        short[] arr2 = { 6, 7 };
  
        // Using Shorts.concat() method which
        // combines arrays from specified
        // arrays into a single array
        short[] arr = Shorts.concat(arr1, arr2);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

Producción :

[3, 4, 5, 6, 7]

Ejemplo 4:

// Java code to show implementation
// of Guava Shorts.contains() method
  
import com.google.common.primitives.Shorts;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        short[] arr = { 3, 4, 5, 6, 7 };
  
        // Using Shorts.contains() method which
        // checks if element is present in array
        // or not
        System.out.println(Shorts.contains(arr, (short)8));
        System.out.println(Shorts.contains(arr, (short)7));
    }
}

producción :

false
true

Ejemplo 5:

// Java code to show implementation
// of Guava Shorts.min() method
  
import com.google.common.primitives.Shorts;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        short[] arr = { 3, 4, 5, 6, 7 };
  
        // Using Shorts.min() method
        System.out.println(Shorts.min(arr));
    }
}

Producción :

3

Ejemplo 6:

// Java code to show implementation
// of Guava Shorts.max() method
  
import com.google.common.primitives.Shorts;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        short[] arr = { 3, 4, 5, 6, 7 };
  
        // Using Shorts.max() method
        System.out.println(Shorts.max(arr));
    }
}

Producción :

7

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *