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

网站建站行业新闻企业营销培训课程

网站建站行业新闻,企业营销培训课程,企业建立网站需要什么条件,哈尔滨精致网站建设目录 七、ArrayList 类 7.1 位置 7.2 特点 7.3 构造方法 7.4 常用方法 7.5 代码举例 7.6 详解 ArrayList 的扩容机制 七、ArrayList 类 7.1 位置 ArrayList 类位于 java.util 包下 7.2 特点 是 List 接口的实现类底层是用一个 Object 数组来存储数据 7.3 构造方法 …

目录

七、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.15wanjia.com/news/2572.html

相关文章:

  • 常德网站建设的策划方案长春网站提升排名
  • 做招聘的网站最新域名8xgmvxyz
  • wordpress网站打开慢免费人脉推广软件
  • wordpress首页文章显示固定分类宁波seo外包引流推广
  • 山东钢结构建设局网站dz论坛seo
  • wordpress 万能表单搜索网站排名优化
  • 福州软件园seo是啥
  • 知名网站建设企业电子商务培训
  • 编程的网站营销策划36计
  • 做网站的外包需要分享客户信息文案代写收费标准
  • 建设通网站的信息是哪里来的百度账号管家
  • 做网站排名费用多少网站开发公司
  • 建网站做站长宁波网络推广方式
  • 安徽党组织标准化建设网站自动化测试培训机构哪个好
  • wordpress多站点不同主题网络营销师官网
  • 网站权重为零关键词分析工具
  • 我国政府网站建设和管理现状湛江seo
  • 佛山营销网站建设推广搜索网站关键词
  • 甘肃企业网站备案网页搜索快捷键是什么
  • 做网站网站百度联盟怎么加入
  • 温州批发网站dz论坛seo设置
  • 网站域名的作用广州关键词seo
  • 东莞高端网站建设收费标准亚洲7号卫星电视
  • 烟台网站建设威企汇互联见效付款网址大全导航
  • 可以做结构图的网站友情链接管理系统
  • 大同网站建设熊掌号江西省seo
  • 青海中小企业网站建设淘宝关键词搜索量查询
  • 房产采集网站源代码网络营销的概念
  • 网站建设建网站最新热点新闻
  • 网站建设三网合一今日国内新闻