List class's toArray in Java- Why can't I convert a list of “Integer” to an “Integer” array?

前端 未结 3 1167
被撕碎了的回忆
被撕碎了的回忆 2021-01-12 07:15

I defined List stack = new ArrayList();

When I\'m trying to convert it to an array in the following way:

         


        
3条回答
  •  清歌不尽
    2021-01-12 07:51

    Do this instead:

    Integer[] array = stack.toArray(new Integer[stack.size()]);
    

    We need to pass the "seed" array as an argument to the toArray method.

提交回复
热议问题