ArrayList的底层实现
package zy809; public class myArrayList {
/** 存放元素 */
private Object[] data;// 创建一个数组引用。 /** 元素的个数 */
private int size;// 一个指标,记录数组的元素个数。 /*
* 三种构造方法。
*/
public myArrayList() {// 构造一个初始为10的空列表
// data = new Object[10];
this(10);
} public myArrayList(myArrayList c) {
//
// data = c.data;
data = new Object[c.size]; for (int i = 0; i < c.size; i++) {
data[i] = c.data[i];
} size = c.size;
} public myArrayList(int n) {// 指定容量
if (n < 0) {
throw new IllegalArgumentException();
}
data = new Object[n];
} /*
* 待实现的方法
*/
public int size() {// 列表的元素数
return size;
} /** 可添加null元素 */
public boolean add(Object o) {// 向滚动列表的末尾添加指定的项。
this.kuoRongJianCe(size + 1);//先扩容数组
data[size++] = o;
return true;
} public Object get(int index) {// 获取 指定位置的的
yueJieJianCe(index, -1);//判断越界 return data[index];
} public void add(int index, Object o) {//指定位置 添加
yueJieJianCe(index, 0); kuoRongJianCe(size + 1); for (int i = size - 1; i >= index; i--) {// 把index~size-1之间的元素后移
data[i + 1] = data[i];
} data[index] = o;
size++;
} public boolean addAll(myArrayList c) { this.kuoRongJianCe(size + c.size); for (int i = 0; i < c.size; i++) {
data[size++] = c.data[i];
} return true;
} public boolean addAll(int index, myArrayList c) {
this.yueJieJianCe(index, 0);
this.kuoRongJianCe(size + c.size);
// 从index位置开始腾挪c.size个空间
int i = size - 1;
for (; i >= index; i--) {// 把集合c中元素依此从index处插入
data[i + c.size] = data[i];
}
// 拷贝c的元素至当前集合
for (int j = 0; j < c.size; j++) {
data[++i] = c.data[j];
} size += c.size; return true;
} public void clear() {
// data = new Object[10];
for (int i = 0; i < size; i++) {
data[i] = null;
}
size = 0;
} public boolean contains(Object o) { return this.indexOf(o) != -1;
} /**
* 如有必要,增加此 MyArrayList 实例的容量,以确保它至少能够容纳最小容量参数所指定的元素数。 参数: minCapacity -
* 所需的最小容量
*/ public void ensureCapacity(int minCapacity) {
int newsize = (int) (1.5 * size) + 1;
if (minCapacity > newsize) {
newsize = minCapacity;
}
Object[] temp = new Object[newsize];
for (int i = 0; i < size; i++) {
temp[i] = data[i];
}
data = temp;
} public int indexOf(Object o) {// 返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。 return this.findElement(o, 0, 1);
} /** 如果此列表中没有元素,则返回 true */
public boolean isEmpty() {
return size == 0;
} public int lastIndexOf(Object o) { return this.findElement(o, size - 1, -1);
} /** 移除指定位置元素,并返回该元素 */
public Object remove(int index) {
this.yueJieJianCe(index, -1);
Object temp = data[index];
//Object []arr=new Object[size-1];
for (int i = index + 1; i < size; i++) {// 把index+1~size-1之间元素往前移
data[i - 1] = data[i];
}
// data=arr;
data[--size] = null; return temp;
} public boolean remove(Object o) {
int index;
if ((index = this.indexOf(o)) != -1) {
this.remove(index); return true;
} return false;
}
/**
* 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。向左移动所有后续 元素(减小其索引)。 此调用将列表缩短了
* (toIndex - fromIndex) 个元素。 (如果 toIndex==fromIndex,则此操作无效。)
*/
public void removeRange(int fromIndex, int toIndex) {
if (fromIndex < 0 || fromIndex >= size || toIndex > size || fromIndex > toIndex) {
throw new IndexOutOfBoundsException();
} if (fromIndex == toIndex)
return; int i = fromIndex;
for (int j = toIndex; j < size; i++, j++) {
data[i] = data[j];
} for (; i < size; i++) {
data[i] = null;
} size -= toIndex - fromIndex;
} /**
* 用指定的元素替代此列表中指定位置上的元素。 参数: index - 要替代的元素的索引 element - 存储在指定位置上的元素 返回:
* 以前位于该指定位置上的元素 抛出: IndexOutOfBoundsException - 如果索引超出范围 (index < 0 ||
* index >= size())
*/
public Object set(int index, Object element) {
this.yueJieJianCe(index, -1);
Object temp = data[index];
data[index] = element;
return temp;
} /**
* 将此 ArrayList 实例的容量调整为列表的当前大小。应用程序可以使用此操作来 最小化 ArrayList 实例的存储量。
*/
public void trimToSize() {
if (size < data.length) {
Object temp[] = new Object[size];
for (int i = 0; i < size; i++) {
temp[i] = data[i];
}
data = temp;
}
} /** 如果列表包含指定 MyArrayList的所有元素,则返回 true */
public boolean containsAll(myArrayList c) {
if (c == null) {
throw new NullPointerException();
}
int temp = c.size;
for (int i = 0; i < temp; i++) {
if (!contains(c.data[i])) {//不包含返回
return false;
}
}
return true;
} /**
* 从列表中移除指定MyArrayList中包含的其所有元素 参数: c - 包含从此列表中移除的元素的 MyArrayList 返回:
* 如果此列表由于调用而发生更改,则返回 true
*/
public boolean removeAll(myArrayList c) { return jiaoji(c, 0);
} /**
* 仅在列表中保留指定 MyArrayList中所包含的元素. 换句话说,该方法从列表中移除未包含在指定 MyArrayList中的所有元素。 参数:
* c - 包含将保留在此列表中的元素的 MyArrayList 返回: 如果此列表由于调用而发生更改,则返回 true
*/
public boolean retainAll(myArrayList c) {
return jiaoji(c, 1);
} /**
*
* @param index
* @param pianYi
* > size, 0; >= size, -1
*/
private void yueJieJianCe(int index, int pianYi) {//封装一个越界判定方法 if (index < 0 || index > size + pianYi) {
throw new IndexOutOfBoundsException("index = " + index+", size= "+size);
}
} private void kuoRongJianCe(int maxsize) {//封装传一个对象时的扩容方法
if (maxsize > data.length) {
this.ensureCapacity((int) (maxsize * 1.5) + 1);
}
} private int findElement(Object o, int qiShi, int buChang) {//封装有一个查找对象方法
for (int i = qiShi; i >= 0 && i < size; i += buChang) {
if ((o != null) ? o.equals(data[i]) : o == data[i]) {
return i;
}
} return -1;
} private boolean jiaoji(myArrayList o, int n) {//封装一个交集问题方法
Object[] arr = new Object[size];
int a1 = size;
int j = 0;
for (int i = 0; i < size; i++) {
if ((n == 0) ? o.contains(data[i]) : !o.contains(data[i])) {
arr[j++] = data[i];
}
}
data = arr;
size = j;
/*
* else if (n == 1) { for (int i = 0; i < size; i++) { if
* (!o.contains(data[i])) { arr[j++] = data[i]; } } data = arr; size =
* j; }
*/
return a1 != size;
}
}
ArrayList的底层实现的更多相关文章
- javaSE基础之 ArrayList的底层简单实现
最近就是想扒一扒存在硬盘里面的学习资料(突然想到什么),把以前写过的一些东西整理一下分享出来. 这边是ArrayList 的简单实现,当然只实现了部分方法 package com.yck.collec ...
- ArrayList的底层实现原理
ArrayList源码分析 1.java.util.ArrayList<E> : List 接口的大小可变数组的实现类 ArrayList 内部基于 数组 存储 各个元素. 所谓大小可变数 ...
- Java 的 ArrayList 的底层数据结构
1. 数据结构--ArrayList源码摘要 ublic class ArrayList<E> extends AbstractList<E> implements List& ...
- JAVA ArrayList集合底层源码分析
目录 ArrayList集合 一.ArrayList的注意事项 二. ArrayList 的底层操作机制源码分析(重点,难点.) 1.JDK8.0 2.JDK11.0 ArrayList集合 一.Ar ...
- 解析ArrayList的底层实现(上)
private static final long serialVersionUID = 8683452581122892189L;//唯一序列号ID private static final int ...
- ArrayList集合底层原理
目录 ArrayList集合特点及源码分析 ArrayList源码分析 成员变量 构造函数 增加方法 add(E e)方法 add(int index, E element)方法 删除方法 remov ...
- JAVA容器-模拟ArrayList的底层实现
概述 ArrayList实质上就是可变数组的实现,着重理解:add.get.set.remove.iterator的实现,我们将关注一下问题. 1.创建ArrayList的时候,默认给数组的长度设置为 ...
- ArrayList 底层实现原理
ArrayList的底层实现原理 1, 属性:private static final int DEFAULT_CAPACITY = 10; private static final Object [ ...
- HashMap、LinkedHashMap、ConcurrentHashMap、ArrayList、LinkedList的底层实现
HashMap:底层是一个数组+链表实现 LinkedHashMap:底层是Hash表和链表的实现 ConcurrentHashMap:基于双数组和链表的Map接口的同步实现 ArrayList:底层 ...
随机推荐
- gradle的安装配置成功标志
gradle主要位于AndroidStudio中 看我的目录 在环境变量里添加用户变量 GRADLE_HOME 然后在环境变量 path 中增加 %GRADLE_HOME%\bin;,如图所示 测试配 ...
- PHP开发APP接口之返回数据
首先说明一下客户端APP通信的格式 1.xml:扩展标记语言(1.用来标记数据,定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言,xml格式统一,跨平台和语言,非常适合数据传输和通信,早已 ...
- 第三十篇-ToolBar的使用
效果图: 创建标题栏,将原本的Title隐藏,并在菜单栏中设置选项. 一.拖入一个ToolBar组件,并在res/values/styles.xml中将原本的标题栏隐藏,添加<item name ...
- ElasticSearch6.5.0 【Rejecting mapping update to [posts] as the final mapping would have more than 1 type】
今天想在一个Index上增加一个type,结果报错 java.lang.IllegalArgumentException: Rejecting mapping update to [posts] as ...
- Educational Codeforces Round 53 (Rated for Div. 2) A Diverse Substring
传送门 https://www.cnblogs.com/violet-acmer/p/10163375.html 题意: 给出串是多态的定义“长度为 n 的串 s ,当串 s 中所有字母出现的次数严格 ...
- 枚举类 enum,结构体类 struct
1.枚举类型的值,直观易于理解,见词知意. 格式: enum 枚举类名:值类型 { 值1, 值2, 值n } 每个值默认(省略“:值类型”)以int型数据存储,从0开始. 使用格式:枚举类名 变量=枚 ...
- JDBC查询MySQL中的表
在数据库test里先创建表school,内容如下 创建接口对象:Statement stmt=con.createStatement(); //创建语句(Statement)ResultSet res ...
- CentOS Bash 命令补全增强软件包 bash-completion
引言 之前安装的 CentOS 7 是最小化安装,在使用 systemctl 命令进行服务的管理时,经常手动输入相关服务名.如果对一个服务名称不熟悉,这样可以迫使我们记住它,但如果对一个服务名已经很熟 ...
- mybatis_异常
1.HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.Binding ...
- go build 和 go run区别