2020-04-28
好久不用Java,(其实现在也不用),只是在看一道之前的LeetCode题(之前用Java写的)的解法,回忆起了一些细节。
特开一贴,记录那些忘了的或者有点模糊的Java细节。
Why is Generic Array Creation not Allowed in Java?
List<String>[] ls = new ArrayList<String>[10]; ❌
List<String>[] ls = new ArrayList[10]; ✅
Why are arrays covariant but generics are invariant?
The reason is that every array knows its element type during runtime, while generic collection doesn't because of type erasure.