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:底层 ...
随机推荐
- Linux安装Gitlab,附iSCSI分区挂载说明
因为Gitlab数据要存放在共享存储,所以本次配置的重头戏倒变成了挂载ISCSI了. OS:CentOS 7.2IP:172.16.1.191/192.168.2.191 iSCSI分Target(服 ...
- bzoj3829 POI2014 FAR-FarmCraft
题目链接 思路 用\(f[i]\)表示完成第\(i\)棵子树所需要得时间. 考虑如果有两个子树\(a\)和\(b\),如果先去完成子树\(a\),那么对于花费得时间就是\(f[b] + siz[a] ...
- 第九篇-新建文件夹和文本文件mkdirs,createNewFile
一.新建一个empty activity的项目 二.修改AndroidMainfest.xml,添加用户权限. <?xml version="1.0" encoding=&q ...
- Day6--Python--小数据池和再谈编码
一.小数据池 参考 目的:缓存我们字符串,整数,布尔值.在使用的时候不需要创建过多的对象 缓存: int, str, bool int: 缓存范围 -5~256 str: 1. 长度小于等于1,直接缓 ...
- 函数式编程 lodash 常用api
1.forEach _.forEach({ 'a': 1, 'b': 2 }, function(value, key) { console.log(key); }); _.forEach([3,4] ...
- python之OpenCv(二)---保存图像
1.使用opencv保存图像 cv2.imwrite(存储路径,图像变量[,存盘标识]) 存盘标识: cv2.CV_IMWRITE_JPEG_QUALITY 设置图片格式为.jpeg或者.jpg的图 ...
- nginx 配置两个域名
1.首先第一步安装: 参考:https://www.cnblogs.com/wyd168/p/6636529.html 启动: /usr/local/nginx/sbin/nginx -c /usr/ ...
- (贪心 map) Flying to the Mars hdu1800
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 基于python调用libvirt API
基于python调用libvirt API 1.程序代码 #!/usr/bin/python import libvirt import sys def createConnection(): con ...
- java FindMyIP.java
s ganymed-ssh2-build210.jar package com.iteye.lindows.ssh.ip; import java.io.BufferedReader; import ...