赞
踩
目录
在集合框架中,List是一个接口,继承自Collection。
Collection也是一个接口,该接口中规范了后序容器中常用的一些方法,具体如下所示:
站在数据结构的角度来看,List就是一个线性表,即n个具有相同类型元素的有限序列,在该序列上可以执行增删改查。
List是个接口,并不能直接用来实例化。
如果要使用,必须去实例化List的实现类。
在集合框架中,ArrayList和LinkedList都实现了List接口。
在集合框架中,ArrayList是一个普通的类,实现了List接口,具体框架图如下:
1. ArrayList实现了RandomAccess接口,表明ArrayList支持随机访问
2. ArrayList实现了Cloneable接口,表明ArrayList是可以clone的
3. ArrayList实现了Serializable接口,表明ArrayList是支持序列化的
4. 和Vector不同,ArrayList不是线程安全的,在单线程下可以使用,在多线程中可以选择Vector或者CopyOnWriteArrayList
5. ArrayList底层是一段连续的空间,并且可以动态扩容,是一个动态类型的顺序表
- public static void main1(String[] args) {
- // 此时底层的数组 大小 默认是0 ,
- ArrayList<Integer> list = new ArrayList<>();
- ArrayList<Integer> list2 = new ArrayList<>(20);
- //当你第一次进行add的时候,此时大小才被分为了10
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(4);
- System.out.println(list);
- for (int i = 0; i < list.size(); i++) {
- System.out.print(list.get(i)+" ");
- }
- System.out.println("=============");
- for (Integer x : list) {
- System.out.print(x+" ");
- }
- }
1. 检测是否真正需要扩容,如果是调用grow准备扩容
2. 预估需要库容的大小
a.初步预估按照1.5倍大小扩容
b.如果用户所需大小超过预估1.5倍大小,则按照用户所需大小扩容
c.真正扩容之前检测是否能扩容成功,防止太大导致扩容失败
3. 使用copyOf进行扩容
- import java.util.Arrays;
-
- public class MyArrayList {
- public int[] elem;//null
- public int usedSize;//0
-
- public MyArrayList() {
- this.elem = new int[5];
- }
-
- // 新增元素,默认在数组最后新增
- public void add(int data) {
- //1、判断是不是满的
- if(isFull()) {
- //扩容
- this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
- }
- this.elem[this.usedSize] = data;
- this.usedSize++;
- }
-
- public boolean isFull() {
- if(this.usedSize == this.elem.length) {
- return true;//满的
- }
- return false;
- }
- // 打印顺序表
- public void myToString() {
- for (int i = 0; i < this.usedSize; i++) {
- System.out.print(this.elem[i]+" ");
- }
- System.out.println();
- }
-
- // 在 pos 位置新增元素
- public void add(int pos, int data) {
- //0、对pos位置进行合法判断
- if(pos < 0 || pos > this.usedSize) {
- System.out.println("pos位置不合法!");
- return;
- }
- //1、不能是满的
- if(isFull()) {
- //扩容
- this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
- }
- //2、正常的插入
- for (int i = this.usedSize-1; i >= pos ; i--) {
- this.elem[i+1] = this.elem[i];
- }
- this.elem[pos] = data;
- this.usedSize++;
- }
- // 判定是否包含某个元素
- public boolean contains(int toFind) {
- for (int i = 0; i < this.usedSize; i++) {
- if(this.elem[i] == toFind) {
- return true;
- }
- }
- return false;
- }
- // 查找某个元素对应的位置
- public int indexOf(int toFind) {
- for (int i = 0; i < this.usedSize; i++) {
- if(this.elem[i] == toFind) {
- return i;
- }
- }
- return -1;//数组没有-1下标
- }
- // 获取 pos 位置的元素
- public int get(int pos) {
- if(pos < 0 || pos >= this.usedSize) {
- System.out.println("pos位置不合法!");
- throw new RuntimeException("pos位置不合法!");
- //return -1;//这里只是检查一下,实际业务处理,这里需要抛异常,自定义的异常
- }
- return this.elem[pos];
- }
- // 给 pos 位置的元素设为 value
- public void set(int pos, int value) {
- if(pos < 0 || pos >= this.usedSize) {
- System.out.println("pos位置不合法!");
- throw new RuntimeException("pos位置不合法!");
- }
- this.elem[pos] = value;//覆盖原来的值
- }
-
- //删除第一次出现的关键字key
- public void remove(int key) {
- if(isEmpty()) {
- System.out.println("空的 不能删除!");
- return;
- }
- int index = indexOf(key);
-
- for (int i = index; i < this.usedSize-1; i++) {
- this.elem[i] = this.elem[i+1];
- }
- this.usedSize--;
- //this.elem[this.usedSize] = null; 如果是引用数据类型,一定要置为空
- }
-
- public boolean isEmpty() {
- if(this.usedSize == 0) {
- return true;
- }
- return false;
- }
- // 获取顺序表长度
- public int size() {
- return this.usedSize;
- }
- // 清空顺序表
- public void clear() {
- // for (int i = 0; i < this.usedSize; i++) {
- // this.elem[i] = null;
- // }
- this.usedSize = 0;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。