java 数组与列表互转

public class MainTest {

    /**
     * Integer[] -> List
     */
    public static void integerArrayToList() {
        Integer[] param = {10,11,999};
        System.out.println("intArrayToIntegerList    :"+param[0]+","+param[1]+","+param[2]);
        List res = Arrays.asList(param);
        System.out.println("intArrayToList res:"+res);
        //res.add(2); 注意:调用Arrays.asList()时,其返回值类型是ArrayList,调用add()时,会报错
        //Arrays.stream(param).collect(Collectors.toList()); 是万能的
    }

    /**
     * List -> Integer[]
     */
    public static void listToIntegerArray() {
        List list = new ArrayList<>();
        list.add(20);list.add(21);list.add(999);
        System.out.println("listToIntegerArray    :"+list);
        Integer[] res = list.toArray(new Integer[0]);
        System.out.println("listToIntegerArray res:"+res[0]+","+res[1]+","+res[2]);

        Integer[] res2 = list.stream().toArray(Integer[]::new);
        System.out.println("listToIntegerArray res:"+res2[0]+","+res2[1]+","+res2[2]);
    }

    /**
     * int[] -> Integer[]
     */
    public static void intArrayToIntegerArray() {
        int[] param = {30,31,999};
        System.out.println("intArrayToIntegerArray    :"+param[0]+","+param[1]+","+param[2]);
        Integer[] res = Arrays.stream(param).boxed().toArray(Integer[]::new);
        System.out.println("intArrayToIntegerArray res:"+res[0]+","+res[1]+","+res[2]);
    }

    /**
     * Integer[] -> int[]
     */
    public static void integerArrayToIntArray() {
        Integer[] param = {40,41,999};
        System.out.println("integerArrayToIntArray    :"+param[0]+","+param[1]+","+param[2]);
        int[] res = Arrays.stream(param).mapToInt(Integer::valueOf).toArray();
        System.out.println("integerArrayToIntArray res:"+res[0]+","+res[1]+","+res[2]);

    }


    /**
     * int[] -> List
     */
    public static void intArrayToList() {
        // 方法1
        int[] param = {50,51,999};
        // List ints = Arrays.asList(param); 无法直接转
        System.out.println("intArrayToList    :"+param[0]+","+param[1]+","+param[2]);
        List res = Arrays.stream(param).boxed().collect(Collectors.toList());
        System.out.println("intArrayToList res:"+res);
    }

    /**
     * List -> int[]
     */
    public static void listToInt() {
        List list = new ArrayList<>();
        list.add(60);list.add(61);list.add(999);
        System.out.println("listToInt:"+list);
        int[] res = list.stream().mapToInt(Integer::valueOf).toArray();
        System.out.println("listToInt res:"+res[0]+","+res[1]+","+res[2]);
    }

    /**
     * int[] -> long[]
     */
    public static void intArrayToLongArray() {
        int[] param = {70,71,999};
        System.out.println("intArrayToLongArray    :"+param[0]+","+param[1]+","+param[2]);
        long[] res = Arrays.stream(param).mapToLong(Long::valueOf).toArray();
        System.out.println("intArrayToLongArray res:"+res[0]+","+res[1]+","+res[2]);
        long[] res2 = Arrays.stream(param).mapToLong(Integer::valueOf).toArray();
        System.out.println("intArrayToLongArray res:"+res2[0]+","+res2[1]+","+res2[2]);
    }

    public static void main(String[] args) {
        //integerArrayToList();
        //listToIntegerArray();
        //intArrayToIntegerArray();
        //integerArrayToIntArray();
        //intArrayToList();
        //listToInt();
        intArrayToLongArray();
    }

}

你可能感兴趣的:(java 数组与列表互转)