当前位置: 首页 > news >正文

java集合篇(七) ---- ArrayList 类

目录

七、ArrayList 类

7.1 位置

7.2 特点

7.3 构造方法

7.4 常用方法

7.5 代码举例

7.6 详解 ArrayList 的扩容机制


七、ArrayList 类

7.1 位置

ArrayList 类位于 java.util 包下

7.2 特点

  1. 是 List 接口的实现类
  2. 底层是用一个 Object 数组来存储数据

7.3 构造方法

public ArrayList()

作用

创建一个空的 ArrayList 对象,初始容量为 0

public ArrayList(int initialCapacity)

作用

创建一个指定初始容量的 ArrayList 对象

public ArrayList(Collection<? extends E> c)

作用

创建一个包含指定集合 c 的 ArrayList 对象

7.4 常用方法

参考 List 接口

List接口

7.5 代码举例

import java.util.ArrayList;
import java.util.Objects;class Student{private int id;private String name;public Student(int id,String name) {this.name = name;this.id = id;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +'}';}// 使用 ArrayList 类存储自定义类时,要重写 equals() 方法// 来确保 contains() 方法和 remove() 方法的正确执行@Overridepublic boolean equals(Object o) {if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return id == student.id && Objects.equals(name, student.name);}
}public class Test03 {public static void main(String[] args) {ArrayList arrayList = new ArrayList();Student student = new Student(1,"张三");Student student2 = new Student(2,"李四");Student student3 = new Student(3,"王五");Student student4 = new Student(4,"小明");Student student5 = new Student(5,"小红");//添加元素arrayList.add(student);arrayList.add(student2);arrayList.add(student3);arrayList.add(student4);arrayList.add(student5);//遍历元素System.out.println("-------------初始遍历-------------");for (Object object : arrayList) {System.out.println(object);}//删除元素//相当于 arrayList.remove(1);//相当于 arrayList.remove(student2);       //没有重写 equals() 方法,这样是可以删除的arrayList.remove(new Student(2,"李四"));      //若没有重写 equals() 方法,这样是删除不了的System.out.println("-------------删除元素后遍历-------------");for (Object object : arrayList) {System.out.println(object);}//替换元素System.out.println("-------------替换元素-------------");Student student6 = new Student(6,"梨花");Object set = arrayList.set(0, student6);System.out.println("替换前的元素为:" + set);//获取指定索引位置的元素Object object = arrayList.get(0);System.out.println("替换后的元素为:" + object);//获取集合的元素个数System.out.println("-------------集合的元素个数-------------");int size = arrayList.size();System.out.println(size);}
}

7.6 详解 ArrayList 的扩容机制

import java.util.ArrayList;
import java.util.Arrays;public class MyJiHe<E> {transient Object[] elementData;private int size;protected transient int modCount = 0;private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};private static final int DEFAULT_CAPACITY = 10;private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;public MyJiHe() {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;}public boolean add(E e) {ensureCapacityInternal(size + 1);elementData[size++] = e;return true;}private void ensureCapacityInternal(int minCapacity) {ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));}private static int calculateCapacity(Object[] elementData, int minCapacity) {if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {return Math.max(DEFAULT_CAPACITY, minCapacity);}return minCapacity;}private void ensureExplicitCapacity(int minCapacity) {modCount++;if (minCapacity - elementData.length > 0)grow(minCapacity);}private void grow(int minCapacity) {int oldCapacity = elementData.length;int newCapacity = oldCapacity + (oldCapacity >> 1);if (newCapacity - minCapacity < 0)newCapacity = minCapacity;if (newCapacity - MAX_ARRAY_SIZE > 0)newCapacity = hugeCapacity(minCapacity);elementData = Arrays.copyOf(elementData, newCapacity);}private static int hugeCapacity(int minCapacity) {if (minCapacity < 0)throw new OutOfMemoryError();return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :MAX_ARRAY_SIZE;}
}


http://www.xdnf.cn/news/1026883.html

相关文章:

  • BeckHoff_FB --> F_SEQ_X3_TrigJob 函数
  • TCP客户端进程分割输入输出
  • 【Qt】工具介绍和信号与槽机制
  • SpringCloud2020-alibaba
  • DDD各种架构详细介绍
  • CLONE——面向长时任务的闭环全身遥操:其MoE架构可实现“蹲着走”,且通过LiDAR里程计和VR跟踪技术解决位置偏差问题
  • 【61 Pandas+Pyecharts | 基于Apriori算法及帕累托算法的超市销售数据分析可视化】
  • 力扣-279.完全平方数
  • 三维重建 —— 3. 单视几何
  • 国产用例管理工具评测:Gitee Test、禅道、蓝凌测试、TestOps 哪家更懂研发协同?
  • 全流程TOUGH系列软件实践技术应用
  • electron-builder打包配置(应用名、安装包、图标、快捷方式、自定义文件关联启动等)
  • Matlab的GUI编程之一
  • 【Android】EventBus详解
  • 基于Redis实现分布式锁
  • 安科瑞亮相2025 SNEC国际太阳能光伏与智慧能源展
  • 7N65-ASEMI智能照明领域专用7N65
  • onxxrunrime问题集锦
  • 黑色金属生产车间:DeviceNet到Modbus RTU网关的“无声桥梁”
  • 【DNS】在 Windows 下修改 `hosts` 文件
  • java哨兵底层原理
  • 社区养老模式:现状、困境与破局之道
  • PH热榜 | 2025-06-13
  • Vim、Nano 与 Emacs 的深度对比及嵌入式开发推荐
  • TIA Portal V20HMI仿真时数值无法写入虚拟plc解决教程
  • SIEMENS 6SL3320-1TG35-8AA3逆变装置
  • SpringCloud-sentinel集成到nacos
  • wireshark抓包过程
  • 《TCP/IP 详解 卷1:协议》第6章:DHCP和自动配置
  • velo2cam_gazebo /velo2cam_calibration 仿真标定测试