问题
I am wondering what is the time complexity [in big O(n)
notations] of ArrayList
to Array
conversion:
ArrayList assetTradingList = new ArrayList();
assetTradingList.add("Stocks trading");
assetTradingList.add("futures and option trading");
assetTradingList.add("electronic trading");
assetTradingList.add("forex trading");
assetTradingList.add("gold trading");
assetTradingList.add("fixed income bond trading");
String [] assetTradingArray = new String[assetTradingList.size()];
assetTradingArray.toArray(assetTradingArray);
similarly, what is the time complexity for arrays to list in the following ways:
method 1 using Arrays.asList
:
String[] asset = {"equity", "stocks", "gold", "foreign exchange","fixed
income", "futures", "options"};
List assetList = Arrays.asList(asset);
method 2 using collections.addAll
:
List assetList = new ArrayList();
String[] asset = {"equity", "stocks", "gold", "foreign exchange", "fixed
income", "futures", "options"};
Collections.addAll(assetList, asset);
method 3 addAll
:
ArrayList newAssetList = new ArrayList();
newAssetList.addAll(Arrays.asList(asset));
The reason I am interested in the overhead of copying back and forth is because in typical interviews, questions come such as given an array of pre-order traversal elements, convert to binary search tree
and so on, involving arrays
. With List
offering a whole bunch of operations such as remove
etc, it would make it simple to code using List
than Array
.
In which case, I would like to defend me for using list
instead of arrays
saying "I would first convert the Array to List because the overhead of this operation is not much (hopefully)".
Any better methods recommended for copying the elements back and forth from array to list
that would be faster would be good know too.
Thanks
回答1:
It would seem that Arrays.asList(T[]);
is the fastest withO(1)
Because the method returns an unmodifiable List
, there is no reason to copy the references over to a new data structure. The method simply uses the given array as a backing array for the unmodifiable List
implementation that it returns.
The other methods seem like they copy each element, one by one to an underlying data structure. ArrayList#toArray(..)
uses System.arraycopy(..)
deep down (O(n)
but faster because it's done natively). Collections.addAll(..)
loops through the array elements (O(n)
).
Careful when using ArrayList
. The backing array doubles in size when its capacity is reached, ie. when it's full. This takes O(n)
time. Adding to an ArrayList
might not be the best idea unless you know how many elements you are adding from the beginning and create it with that size.
回答2:
Since the backing data structure of ArrayList
is an array, and copying of an array elements is a O(n), it is O(n).
The only overhead I see is pollution of heap with those intermediate objects. Most of the time developers (especially, beginners) don't care about that and treat Java GC as a magic wand that cleans everything after them. My personal opinion is, if you can avoid unwanted transformation of array to list and vice versa, do that.
If you know beforehand the foreseeable (e.g. defined) size of a list, preallocate its size with ArrayList(int size)
constructor to avoid internal array copying that takes place inside ArrayList
when capacity is exhausted. Depending on a use case, consider other implementations, e.g. LinkedList
, if you're only interested in consequent addition to the list, and iterative reading.
回答3:
An ArrayList
is fundamentally just a wrapper around an Object[]
array. It has a lot of helpful methods for doing things like finding items, adding and removing items, and so on, but from a time complexity perspective these are no better than doing the operations yourself on a plain array.
If your hope is that an ArrayList
is fundamentally more efficient than a manually managed array, it's not. Sorry!
Converting an array to an
ArrayList
takes O(n) time. Every element must be copied.Inserting or removing an element takes O(m) amortized time, where m is the number of elements following the insertion/removal index. These elements have to be moved to new indices whether you use an array or
ArrayList
."Amortized" means average -- sometimes the backing array will need to be grown or shrunk, which takes additional time on the order of O(n). This doesn't happen every time, so on the whole the additional time averages out to an O(1) additional cost.
Accessing an element at an arbitrary index takes O(1) time. Both provide constant time random access.
来源:https://stackoverflow.com/questions/21122143/what-is-the-time-complexity-for-copying-list-back-to-arrays-and-vice-versa-in-ja