In Java ArrayList<E> implementation base on a array of objects.
Can anybody explain me why implementation of ArrayList<E> uses array Object[] for data storage instead of E[]? What the benefit of using Object[]?
In Java ArrayList<E> implementation base on a array of objects. Can anybody explain me
Share
In Java, creating an array of a generic type is not straightforward.
The simple approach does not compile:
Replace
EwithObject, and all is well (at the expense of added complexity elsewhere in the container implementation).There are alternative approaches, but they present a different set of tradeoffs. For an extensive discussion, see How to create a generic array in Java?