Guava Shorts类

Guava Shorts类

Shorts是原始类型short的一个实用类。它提供了与短基元有关的静态实用方法,这些方法在Short或Arrays中都没有找到。

声明 :

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

下表显示了Guava Shorts Class的字段总结。

Guava Shorts类

Guava Shorts Class提供的一些方法有: 异常情况。

Guava Shorts类

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

Guava Shorts类

下表显示了Guava Shorts Class提供的一些其他方法:下面给出的一些例子显示了Guava Shorts Class方法的实现:例子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);
    }
}

输出:

[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

赞(0)

猜你喜欢

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址
    Guava 教程
    Guava 教程Guava - 概述Guava - 环境搭建Guava - Optional类Guava - Preconditions类Guava - Ordering类Guava - Objects类Guava - Range类Guava - Throwables类Guava - Collections 工具类Guava - Caching 工具类Guava - Multiset接口Guava - bimapGuava - MultiMap
    Guava Ints类
    Guava Ints类Guava Ints - concat() 函数Guava Ints - lastIndexOf() 函数Guava Ints - contains() 函数Guava Ints - asList() 函数Guava Ints - max() 函数Guava Ints - min() 函数Guava Ints - join() 函数Guava Ints - toArray() 函数Guava Ints - indexOf() 函数
    Guava Chars类
    Guava Chars类Guava - Chars.compare()方法与实例Guava - Chars.hashCode()方法与实例Guava - Chars.min()方法与实例Guava - Chars.max()方法与实例Guava - Chars.toArray()方法与实例Guava - Chars.concat()方法与实例Guava - Chars.contains()方法与实例Guava - Chars.lastIndexOf()方法与实例Guava - Chars.join()方法与实例Guava - Chars.indexOf()方法与实例Guava - Chars.asList()方法与实例
    Guava LongMath类
    Guava LongMath类Guava - LongMath.binomial方法与实例Guava - LongMath.log2方法与实例Guava - LongMath.pow方法与实例Guava - LongMath.isPowerOfTwo方法与实例Guava - LongMath.gcd方法与实例Guava - LongMath.mean方法与实例Guava - LongMath.sqrt方法与实例Guava - LongMath.mod方法与实例Guava - LongMath.log10方法与实例Guava - LongMath.checkedMultiply方法与实例Guava - LongMath.checkedAdd方法与实例Guava - LongMath.checkedPow方法与实例Guava - LongMath.divide方法与实例
    Guava Longs类
    Guava Longs类Guava - Longs.compare()方法与实例Guava - Longs.hashCode()方法与实例Guava - Longs.factorial方法与实例Guava - Longs.contains()方法与实例Guava - Longs.min()方法与实例Guava - Longs.max()方法与实例Guava - Longs.toArray()方法与实例Guava - Longs.lastIndexOf()方法与实例Guava - Longs.join()方法与实例Guava - Longs.asList()方法与实例Guava - Longs.checkedSubtract()方法与实例Guava - Longs.concat()方法与实例Guava - Longs.indexOf()方法与实例
    Guava Booleans类
    Guava Booleans类Guava - Booleans.asList()方法及示例Guava - Booleans.compare()方法及示例Guava - Booleans.concat()方法及示例Guava - Booleans.contains()方法及示例Guava - Booleans.countTrue()方法及示例Guava - Booleans.hashCode()方法及示例Guava - Booleans.indexOf()方法及示例Guava - Booleans.join()方法及示例Guava - Booleans.lastIndexOf()方法及示例Guava - Booleans.toArray()方法及示例
    Guava Shorts类
    Guava Shorts类Guava - Shorts.toArray()方法及实例Guava - Shorts.min()方法及实例Guava - Shorts.max()方法及实例Guava - Shorts.lastIndexOf()方法及实例Guava - Shorts.join()方法及实例Guava - Shorts.indexOf()方法及实例Guava - Shorts.hashCode()方法及实例Guava - Shorts.contains()方法及实例Guava - Shorts.concat()方法及实例Guava - Shorts.asList()方法及实例
    Guava IntMath类
    Guava IntMath类Guava - IntMath.checkedAdd()方法及实例Guava - IntMath.checkedMultiply()方法及实例Guava - IntMath.checkedPow()方法及实例Guava - IntMath.checkedSubtract()方法及实例Guava - IntMath.divide()方法及实例Guava - IntMath.log10()方法及实例