1. 什么是泛型?(很经典的总结,重要)
- 将类型变成参数,提高代码的复用率
2. 有泛型类型 Box<T>,那么 Box<Integer> 是 Box<Number> 的子类吗?
- 不是,同是 Box 类型,不存在子类关系

3. List、Set、Map 的特点?(先记录一下,后面慢慢理解)

4. Java 中普通数组的局限性?(至少说 2 点)

5. Java 中遍历数组的方式?(至少说 3 种)

6. 为什么很多集合可以调用 for-each
?为什么很多集合可以调用 Iterator
迭代器?
- 因为
for-each
的本质就是 调用Iterator
迭代器 - 而几乎所有集合都实现了
Iterable
接口


7. 如何自定义一个迭代器?(以后用到再回来看)

- 调用代码

8. ArrayList 的动态扩容原理?(从内存结构图来讲,重要)

9. 一个细节:当我们判断一个数字 x 是否为偶数时,我们通常会使用 x % 2 == 0
来判断,这种方式可以优化吗?
- 可以优化:使用
x & 1 == 0
这种位运算
,效率会更高
10. 在 Java 中,如果我们在遍历元素的时候,要删除或者添加元素,用哪个方式比较好?

11. 如果在迭代器中调用 list.remove
方法,就是使用集合自带的方法修改集合长度,可能会抛出 java.util.ConcurrentModificationException
异常,迭代器是如何判断集合被并发修改的?
- 集合内部维护了一个
modCounter
的成员变量,每当 add 或者 remove 时,会调用modCounter++
。 - 所以迭代器是依靠
modCounter
是否发生改变,从而抛出异常的
网友评论