Java中String[]类型与ArrayList类型之间转换

list.toArray(new String[]{}); Arrays.asList(stringArray)

Posted by PanMin on September 13, 2016

Java中String[]类型与ArrayList类型之间转换

  1. List转为String[]
    List list = new ArrayList();
    String[] stringArray = new String[2]; 
    list.toArray(stringArray);    
    //String str[] = list.toArray(new String[]{}); 
    
  2. String[]转为List
    String[] stringArray = new String[2]; 
    List list = java.util.Arrays.asList(stringArray);
    
  3. remove() on List created by Arrays.asList() throws UnsupportedOperationException
    直接使用Arrays.asList(la)转成List<> 使用remove时会报错,所以可以有一下用法:
    List<String> c = new ArrayList<>(Arrays.asList(la));