Java Ints asList() 函数

来源:这里教程网 时间:2026-02-17 21:41:55 作者:

Java Ints asList() 函数

Guava的 Ints .asList() 返回一个由指定数组支持的固定大小的列表。

语法

public static List<Integer>     asList(int[] array)

参数。该方法以数组作为参数,数组是支持列表的数组。

返回值: 该方法返回一个由指定数组支持的固定大小的列表。

例子 1 :

// Java code to show implementation of// Guava's Ints.asList() method  import com.google.common.primitives.Ints;import java.util.List;  class GFG {      // Driver's code    public static void main(String[] args)    {          // Creating an integer array        int arr[] = { 1, 2, 3, 4, 5 };          // Using Ints.asList() method to wrap        // the specified primitive Integer array        // as a List of the Integer type        List<Integer> myList = Ints.asList(arr);          // Displaying the elements in List        System.out.println("List of given array: "                           + myList);    }}

输出:

List of given array: [1, 2, 3, 4, 5]

例2 :

// Java code to show implementation of// Guava's Ints.asList() method  import com.google.common.primitives.Ints;import java.util.List;  class GFG {      // Driver's code    public static void main(String[] args)    {        // Creating an integer array        int arr[] = { 3, 5, 7 };          // Using Ints.asList() method to wrap        // the specified primitive Integer array        // as a List of the Integer type        List<Integer> myList = Ints.asList(arr);          // Displaying the elements in List        System.out.println("List of given array: "                           + myList);    }}

输出:

List of given array: [3, 5, 7]

参考资料: https://google.github.io/guava/releases/22.0/api/docs/com/google/common/primitives/Ints.html#asList-int…-

相关推荐