Java List和Array之间的转换

import java.util.Arrays;
import java.util.List;

class Test {
    //Object数组向List的转换
    public static List<String> testArray2List(String[] args) {
        return Arrays.asList(args);
    }

    public static String[] testList2Array(List<String> args){
        List<String> list2 = Arrays.asList("aaa", "bbb", "ccc");
        String[] array = list2.toArray(new String[list2.size()]);

        System.out.println(array);
        for (String s : array){
            System.out.println(s);
        }
        return args.toArray(new String[args.size()]);
    }

    public static void main(String[] args) {
        List<String> list1 = Test.testArray2List(new String[]{"aaa", "bbb", "ccc"});
        System.out.println(list1);
  }

}

输出

[aaa, bbb, ccc]
上一篇:ArrayList与顺序表


下一篇:python3合并排序