我有一个 ArrayList,它看起来像这样: Index1: Item1 Index2: Item2 Index3: Item3
如果我删除一个 item (ArrayList.remove(2)),我会得到这样的东西:
Index1: Item1
Index2: Item2
Index3: null
用 a 遍历这个for-loop给了我一个null pointer. 所以我使用ArrayList.trimToSize(),这给了我:
Index1: Item1
Index2: Item2
喜悦。除了 forloop仍然给我一个null pointer. 如果我用 a 包围它try-catch并检查它,看起来 Java 已经将我的ArrayList背部扩展到其原始大小,并带有null:
Index1: Item1
Index2: Item2
Index3: null
这里发生了什么?为什么我trimToSize()的被反转了?我知道有(很多)其他方法可以迭代某些东西,但我想知道发生了什么以及为什么。