How to convert an ArrayList containing Integers to primitive int array?

前端 未结 18 1587
情书的邮戳
情书的邮戳 2020-11-22 11:23

I\'m trying to convert an ArrayList containing Integer objects to primitive int[] with the following piece of code, but it is throwing compile time error. Is it possible to

18条回答
  •  清歌不尽
    2020-11-22 11:43

       List list = new ArrayList();
    
        list.add(1);
        list.add(2);
    
        int[] result = null;
        StringBuffer strBuffer = new StringBuffer();
        for (Object o : list) {
            strBuffer.append(o);
            result = new int[] { Integer.parseInt(strBuffer.toString()) };
            for (Integer i : result) {
                System.out.println(i);
            }
            strBuffer.delete(0, strBuffer.length());
        }
    

提交回复
热议问题