Convert an ArrayList to an object array

后端 未结 6 1119
情深已故
情深已故 2020-12-13 09:20

Is there a command in java for conversion of an ArrayList into a object array. I know how to do this copying each object from the arrayList into the object array, but I was

6条回答
  •  情书的邮戳
    2020-12-13 09:55

    Convert an ArrayList to an object array

    ArrayList has a constructor that takes a Collection, so the common idiom is:

    List list = new ArrayList(Arrays.asList(array));
    

    Which constructs a copy of the list created by the array.

    now, Arrays.asList(array) will wrap the array, so changes to the list will affect the array, and visa versa. Although you can't add or remove

    elements from such a list.

提交回复
热议问题