美文网首页
集合、数组转换

集合、数组转换

作者: 是新来的啊强呀 | 来源:发表于2020-06-07 13:40 被阅读0次

数组==>集合
使用 Arrays 类提供的方法快速转换,Arrays.asList();

public class ArrayToList {
    public static void main(String[] args) {
        String[] arrs = new String[3];
        arrs[0] ="111";
        arrs[1] ="222";
        arrs[2] ="333";
        
        List<String> list = Arrays.asList(arrs);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}

集合==>数组

        ArrayList<Integer> temp = new ArrayList<>();
        temp.add(0);
        temp.add(1);
        temp.add(2);
        temp.add(3);
        temp.add(4);
        temp.add(5);
        Integer[] res = temp.toArray(new Integer[]{});
        System.out.print(Arrays.toString(res));
// Integer集合转Integer数组,注意Integer集合不能直接转为int数组,只能通过遍历转换。
        int[] t = new int[res.length];
        System.out.print(Arrays.toString(res));
        for(int i=0;i<res.length-1;i++){
            t[i] = res[i];
        }

相关文章

网友评论

      本文标题:集合、数组转换

      本文链接:https://www.haomeiwen.com/subject/lkkctktx.html