数组转 List ,使用 JDK 中 java.util.Arrays 工具类的 asList 方法

public static void testArray2List() {
    String[] strs = new String[] {"aaa", "bbb", "ccc"};
    List<String> list = Arrays.asList(strs);
    for (String s : list) {
        System.out.println(s);
    }
}

 

 

List 转数组,使用 List 的toArray方法。无参toArray方法返回Object数组,传入初始化长度的数组对象,返回该对象数组

public static void testList2Array() {
    List<String> list = Arrays.asList("aaa", "bbb", "ccc");
    String[] array = list.toArray(new String[list.size()]);
    for (String s : array) {
        System.out.println(s);
    }
}

 

 

  

来一道刷了进BAT的面试题?

相关文章:

  • 2021-07-25
  • 2021-10-02
  • 2021-12-05
  • 2022-01-19
  • 2022-12-23
猜你喜欢
  • 2021-07-30
  • 2022-02-02
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2021-05-25
相关资源
相似解决方案