记录Java中的几个Array和List转换技巧

以此记录作为2016的结束吧,祝大家2017年收货更好的自己

int型数组转换为List<Integer>对象:

    static List<Integer> intArrayToListInteger(int[] ints) {
        List<Integer> intList = new ArrayList<>(ints.length);
        for (int anInt : ints) {
            intList.add(anInt);
        }
        return intList;
    }

List<Integer>对象转换为int型数组:

    static int[] listIntegerToIntArray(List<Integer> list) {
        int[] ints = new int[list.size()];
        for (int i = 0; i < list.size(); ++i) {
            ints[i] = list.get(i);
        }
        return ints;
    }

List<Integer>对象转换为List<String>对象:

    static List<String> listIntegerToListString(List<Integer> integerList) {
        List<String> stringList = new ArrayList<>(integerList.size());
        for (Integer anInt : integerList) {
            stringList.add(String.valueOf(anInt));
        }
        return stringList;
    }

List<String>对象转换为List<Integer>对象:

    static List<Integer> listStringToListInteger(List<String> stringList) {
        List<Integer> intList = new ArrayList<>();
        for (String s : stringList) {
            intList.add(Integer.valueOf(s));
        }
        return intList;
    }

String型数组转换为List<String>对象:

    static List<String> stringArrayToListInteger(String[] strings) {
        List<String> stringList = new ArrayList<>(Arrays.asList(strings));
        return stringList;
    }

List<String>对象转换为String型数组:

    static String[] listStringToStringArray(List<String> stringList){
        String[] strings = stringList.toArray(new String[0]);
        return strings;
    }

祝好

    原文作者:赵者也
    原文地址: https://www.jianshu.com/p/17f93dbddfb1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞