31

What is the cost of using Arrays.asList to convert static object arrays? Assuming that the object array has N items is it just an O(N) algorithm, where all of the items are copied by reference or is it merely a facade where the original array is put behind a List facade?

0

1 Answer 1

48

It is cheap, O(1). As you suspect the list is merely a wrapper around the array. This is confirmed by the Java API documentation:

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

2
  • When going the other direction (Collection.toArray) the array is not "write-through". Changes to the array won't affect the list the array came from.
    – Sam Barnum
    Oct 12, 2009 at 14:46
  • In otherwords sam, it clones the original array.
    – monksy
    May 30, 2012 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.