toArray()Ints类的方法返回一个数组,其中包含每个collection的值,并以Number.intValue()的方式转换为int值。以下是语法-public static int[]
toArray(Collection extends Number> collection)
在这里,参数是Number实例的集合
我们首先来看一个例子-
示例import java.util.Arrays;
import java.util.List;
import com.google.common.primitives.Ints;
class Demo {
public static void main(String[] args) {
List list = Arrays.asList(20, 40, 60, 80, 100, 120, 140, 160);
System.out.println("List = "+list);
int[] myArr = Ints.toArray(list);
System.out.println("Array (from list) = " + Arrays.toString(myArr));
}
}
输出结果List = [20, 40, 60, 80, 100, 120, 140, 160]
Array = [20, 40, 60, 80, 100, 120, 140, 160]