如何转换列表<Integer>到 Java 中的 int[]? [复制] - How to convert List<Integer> to int[] in Java? [duplicate]

问题:

This question already has answers here : 这个问题在这里已经有了答案
Closed 2 years ago . 2年前关闭。

This is similar to this question: How to convert int[] to Integer[] in Java?这类似于这个问题: How to convert int[] to Integer[] in Java?

I'm new to Java.我是 Java 新手。 How can i convert a List<Integer> to int[] in Java?如何在 Java 中将List<Integer>转换为int[] I'm confused because List.toArray() actually returns an Object[] , which can be cast to nether Integer[] or int[] .我很困惑,因为List.toArray()实际上返回一个Object[] ,它可以转换为List.toArray() Integer[]int[]

Right now I'm using a loop to do so:现在我正在使用循环来做到这一点:

int[] toIntArray(List<Integer> list){
  int[] ret = new int[list.size()];
  for(int i = 0;i < ret.length;i++)
    ret[i] = list.get(i);
  return ret;
}

I'm sure there's a better way to do this.我相信有更好的方法来做到这一点。


解决方案:

参考一: https://en.stackoom.com/question/41qp
参考二: https://stackoom.com/question/41qp
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章