casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

后端 未结 7 2041
忘了有多久
忘了有多久 2020-11-29 08:17

I\'m new to Java and am trying to understand why the first code snippet doesn\'t cause this exception but the second one does. Since a string array is passed into Arrays.as

7条回答
  •  失恋的感觉
    2020-11-29 08:23

    No need to cast manually. This simple code may help you,

    List stuff = new ArrayList();
    String line = "a,b,cdef,g";
    String delim = ",";
    stuff.addAll(Arrays.asList(line.split(delim)));
    

提交回复
热议问题