Guava – Longs.toArray()方法与实例

Guava – Longs.toArray() 方法与实例

The toArray() method of Guava库中的Longs类 ,用于将作为参数传递给该方法的长值转换成长数组。这些长值被作为一个集合传递给这个方法。该方法返回一个Long数组。

语法:

public static long[] toArray(Collection<? extends Number> collection)

参数: 这个方法接受一个强制参数collection,它是要转换为Long数组的长值集合。

返回值: 该方法返回一个长数组,包含与集合相同的值,顺序相同。

异常情况: 如果传递的集合或其任何元素为空,该方法会抛出NullPointerException。

以下程序说明了toArray()方法的使用。

示例1:

// Java code to show implementation of
// Guava's Longs.toArray() method
  
import com.google.common.primitives.Longs;
import java.util.Arrays;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a List of Longs
        List<Long> myList
            = Arrays.asList(1L, 2L, 3L, 4L, 5L);
  
        // Using Longs.toArray() method to convert
        // a List or Set of Long to an array of Long
        long[] arr = Longs.toArray(myList);
  
        // Displaying an array containing each
        // value of collection,
        // converted to a long value
        System.out.println(Arrays.toString(arr));
    }
}

输出:

[1, 2, 3, 4, 5]

示例2:

// Java code to show implementation of
// Guava's Longs.toArray() method
  
import com.google.common.primitives.Longs;
import java.util.Arrays;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        try {
            // Creating a List of Longs
            List<Long> myList
                = Arrays.asList(2L, 4L, null);
  
            // Using Longs.toArray() method
            // to convert a List or Set of Long
            // to an array of Long.
            // This should raise "NullPointerException"
            // as the collection contains "null"
            // as an element
            long[] arr = Longs.toArray(myList);
  
            // Displaying an array containing each
            // value of collection,
            // converted to a long value
            System.out.println(Arrays
                                   .toString(arr));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

输出:

java.lang.NullPointerException

参考:

https://google.github.io/guava/releases/21.0/api/docs/com/google/common/primitives/Longs.html#toArray-java.util.Collection-

赞(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()方法及实例