📜  短裤类 |番石榴 |Java

📅  最后修改于: 2022-05-13 01:55:17.641000             🧑  作者: Mango

短裤类 |番石榴 |Java

Shorts是原始类型 short 的实用程序类。它提供了与短原语有关的静态实用方法,这些方法在短原语或数组中都没有。

宣言 :

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

下表显示了番石榴短裤类的字段摘要:


Guava Shorts Class 提供的一些方法是:

例外:

  • checkedCast : IllegalArgumentException如果值大于 Short.MAX_VALUE 或小于 Short.MIN_VALUE
  • min : IllegalArgumentException如果数组为空。
  • max : IllegalArgumentException如果数组为空。
  • fromByteArray : IllegalArgumentException如果 bytes 的元素少于 2 个。
  • ensureCapacity : IllegalArgumentException如果 minLength 或 padding 为负数。
  • toArray : NullPointerException如果集合或其任何元素为空。

下表显示了 Guava Shorts Class 提供的一些其他方法:

下面给出了一些示例,展示了 Guava Shorts 类方法的实现:
示例 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 myList = Shorts.asList(arr);
  
        // Displaying the elements
        System.out.println(myList);
    }
}

输出 :

[3, 4, 5, 6, 7]

示例 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));
    }
}

输出 :

2

示例 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));
    }
}

输出 :

[3, 4, 5, 6, 7]

示例 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));
    }
}

输出 :

false
true

示例 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));
    }
}

输出 :

3

示例 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));
    }
}

输出 :

7