-
可以很明显的看出,ArrayList 继承了AbstractList,实现了RandomAccess、Cloneable、java.io.Serializable。
-
点到List源码中看一看:
-
-
既然实现了Cloneable接口,那么代表了它覆盖了函数clone(),证明他是可以被克隆的。
-
既然实现了java.io.Serializable接口,意味着它支持序列化,能通过序列化进行传输。
-
我们再看一看AbstractList的继承链和源码:
-
-
-
呀,这玩意怎么也实现了List,那为什么我们ArrayList已经继承了AbstractList还要继续实现List呢?
-
我们自己模拟一个类似这种的实现来看看(此模拟过程参考了其他博主的文章,鉴于不是掘金的文章,这里不贴链接了。)
public class Test {
public static interface MyInterface {
void foo();
}
public static class BaseClass implements MyInterface, Cloneable, Serializable {
@Override
public void foo() {
System.out.println(“BaseClass.foo”);
}
}
public static class Class1 extends BaseClass {
@Override
public void foo() {
super.foo();
System.out.println(“Class1.foo”);
}
}
static class Class2 extends BaseClass implements MyInterface, Cloneable,
Serializable {
@Override
public void foo() {
super.foo();
System.out.println(“Class2.foo”);
}
}
public static void main(String[] args) {
showInterfacesFor(BaseClass.class);
showInterfacesFor(Class1.class);
showInterfacesFor(Class2.class);
}
private static void showInterfacesFor(Class<?> clazz) {
System.out.printf(“%s --> %s\n”, clazz, Arrays.toString(clazz
.getInterfaces()));
}
}
-
输出结果如下
-
-
从结果可以看出虽然Class1类的父类实现了接口,但是本身并没有再次实现接口,因此通过java.lang.Class直接获取Class1类的接口为空数组。
-
因此在实现代理的时候就会出现问题。所以可能是设计者就是特意这样设计的嘞?有特别深入了解者,可以解惑下!
-
唔,这里就点到为止吧,我不知道这个文章需要详细到什么地步?需要贴具体的add和remove的源码讲解嘛?
-
因此,它适合随机查找和遍历,不适合插入和删除。
四、Vector
-
Vector 与 ArrayList 一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写 Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费。因此,访问它比访问 ArrayList 慢。
-
点击去看一看源码
-
-
继承了AbstractList,实现了List;所以,它是一个队列,支持相关的添加、删除、修改、遍历等功能。
-
实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在Vector中,我们即可以通过元素的序号快速获取元素对象;这就是快速随机访问。
-
实现了Cloneable接口,即实现clone()函数。它能被克隆。
-
我们看下Vector的构造函数:
/**
-
Constructs an empty vector with the specified initial capacity and
-
capacity increment.
-
@param initialCapacity the initial capacity of the vector
-
@param capacityIncrement the amount by which the capacity is
-
increased when the vector overflows
-
@throws IllegalArgumentException if the specified initial capacity
-
is negative
capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。
*/
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
/**
-
Constructs an empty vector with the specified initial capacity and
-
with its capacity increment equal to zero.
-
@param initialCapacity the initial capacity of the vector
-
@throws IllegalArgumentException if the specified initial capacity
-
is negative
capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。
*/
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
/**
-
Constructs an empty vector so that its internal data array
-
has size {@code 10} and its standard capacity increment is
-
zero.
默认构造函数
*/
public Vector() {
this(10);
}
/**
-
Constructs a vector containing the elements of the specified
-
collection, in the order they are returned by the collection’s
-
iterator.
-
@param c the collection whose elements are to be placed into this
-
vector
-
@throws NullPointerException if the specified collection is null
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
最后
分享一些系统的面试题,大家可以拿去刷一刷,准备面试涨薪。
这些面试题相对应的技术点:
- JVM
- MySQL
- Mybatis
- MongoDB
- Redis
- Spring
- Spring boot
- Spring cloud
- Kafka
- RabbitMQ
- Nginx
- …
大类就是:
- Java基础
- 数据结构与算法
- 并发编程
- 数据库
- 设计模式
- 微服务
- 消息中间件
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-nr96meI6-1712761822514)]
[外链图片转存中…(img-fjVC14FL-1712761822514)]
[外链图片转存中…(img-Zjz0o2ZB-1712761822514)]
[外链图片转存中…(img-FV0V2cyj-1712761822514)]
[外链图片转存中…(img-34HcX62s-1712761822514)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-wuMcJxVx-1712761822515)]