Java 中的 Ints asList() 函数
java 8object oriented programmingprogramming更新于 2025/12/3 19:56:17
Java Guava 中 Ints 类的 asList() 方法返回一个由指定数组支持的固定大小列表。语法如下 −
public static List<Integer> asList(int[] arr)
这里,arr 是支持列表的数组。
让我们看一个实现 Ints 类 asList() 方法的示例 −
示例
import com.google.common.primitives.Ints;
import java.util.List;
class Demo {
public static void main(String[] args) {
int arr[] = { 20, 30, 40, 60, 80, 90, 120, 150 };
System.out.println("Array elements = ");
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
List<Integer> list = Ints.asList(arr);
System.out.println("List = "+ list);
}
}
输出
Array = 20 30 40 60 80 90 120 150 List = [20, 30, 40, 60, 80, 90, 120, 150]

