This question already has answers here:
How can I convert a Java HashSet<Integer> to a primitive int array?
(8个答案)
3年前关闭。
我正在使用以下代码将Set转换为int []
我收到以下错误:
在不使用for循环一一添加元素的情况下进行转换的最干净方法是什么?谢谢!
(8个答案)
3年前关闭。
我正在使用以下代码将Set转换为int []
Set<Integer> common = new HashSet<Integer>();
int[] myArray = (int[]) common.toArray();
我收到以下错误:
error: incompatible types: Object[] cannot be converted to int[]
在不使用for循环一一添加元素的情况下进行转换的最干净方法是什么?谢谢!
最佳答案
Set<Integer> common = new HashSet<>();
int[] values = Ints.toArray(common);