Assigning an array to an ArrayList in Java

后端 未结 4 1456
长发绾君心
长发绾君心 2020-12-01 14:09

Is it possible to assign an array to an ArrayList in Java?

4条回答
  •  一向
    一向 (楼主)
    2020-12-01 14:59

    You can use Arrays.asList():

    Type[] anArray = ...
    ArrayList aList = new ArrayList(Arrays.asList(anArray));
    

    or alternatively, Collections.addAll():

    ArrayList aList = new ArrayList();
    Collections.addAll(theList, anArray); 
    

    Note that you aren't technically assigning an array to a List (well, you can't do that), but I think this is the end result you are looking for.

提交回复
热议问题