Why does Java ArrayList use per-element casting instead of per-array casting?

前端 未结 4 689
梦如初夏
梦如初夏 2021-01-07 18:47

What happens inside Java\'s ArrayList (and probably many other classes) is that there is an internal Object[] array = new Object[n];, to w

4条回答
  •  既然无缘
    2021-01-07 19:05

    I think is more a matter of code style not of performance or type safety (because the backing array is private)

    The java 5 ArrayList was implemented the way you suggested with an E[] array. If you look at the source code you see that it contains 7 (E[]) casts. From java 6 the ArrayList changed to use an Object[] array which resulted in only 3 (E) casts.

提交回复
热议问题