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的底层实现的更多相关文章

  1. javaSE基础之 ArrayList的底层简单实现

    最近就是想扒一扒存在硬盘里面的学习资料(突然想到什么),把以前写过的一些东西整理一下分享出来. 这边是ArrayList 的简单实现,当然只实现了部分方法 package com.yck.collec ...

  2. ArrayList的底层实现原理

    ArrayList源码分析 1.java.util.ArrayList<E> : List 接口的大小可变数组的实现类 ArrayList 内部基于 数组 存储 各个元素. 所谓大小可变数 ...

  3. Java 的 ArrayList 的底层数据结构

    1. 数据结构--ArrayList源码摘要 ublic class ArrayList<E> extends AbstractList<E> implements List& ...

  4. JAVA ArrayList集合底层源码分析

    目录 ArrayList集合 一.ArrayList的注意事项 二. ArrayList 的底层操作机制源码分析(重点,难点.) 1.JDK8.0 2.JDK11.0 ArrayList集合 一.Ar ...

  5. 解析ArrayList的底层实现(上)

    private static final long serialVersionUID = 8683452581122892189L;//唯一序列号ID private static final int ...

  6. ArrayList集合底层原理

    目录 ArrayList集合特点及源码分析 ArrayList源码分析 成员变量 构造函数 增加方法 add(E e)方法 add(int index, E element)方法 删除方法 remov ...

  7. JAVA容器-模拟ArrayList的底层实现

    概述 ArrayList实质上就是可变数组的实现,着重理解:add.get.set.remove.iterator的实现,我们将关注一下问题. 1.创建ArrayList的时候,默认给数组的长度设置为 ...

  8. ArrayList 底层实现原理

    ArrayList的底层实现原理 1, 属性:private static final int DEFAULT_CAPACITY = 10; private static final Object [ ...

  9. HashMap、LinkedHashMap、ConcurrentHashMap、ArrayList、LinkedList的底层实现

    HashMap:底层是一个数组+链表实现 LinkedHashMap:底层是Hash表和链表的实现 ConcurrentHashMap:基于双数组和链表的Map接口的同步实现 ArrayList:底层 ...

随机推荐

  1. eclipse 设置

    修改工作空间默认编码:Window --> Preferences --> General --> Workspace --> Text file encoding --> ...

  2. win10开机自启动

    快捷方式复制到win+ R 编辑shell:startup enter后的文件夹中

  3. linux系统调用之网络管理1

    getdomainname 取域名 setdomainname 设置域名 gethostid 获取主机标识号 sethostid 设置主机标识号 gethostname 获取本主机名称 sethost ...

  4. 实现迁徙学习-《Tensorflow 实战Google深度学习框架》代码详解

    为了实现迁徙学习,首先是数据集的下载 #利用curl下载数据集 curl -o flower_photos.tgz http://download.tensorflow.org/example_ima ...

  5. JSP页面用<a>标签访问 Action 出错

    问题: JSP页面 <a href="/crud1/crud1/add.action" >添加</a> struts.xml 中: <package ...

  6. 91 Testing MySQL学习总结

    91 Testing MySQL学习总结 --------- 在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System什么是数据库?数据 ...

  7. docker swarm 简易版

    节点名称 相关服务 ip地址 master1/node1 swarm manager(master) / consul 192.168.132.131 master2/node2 swarm mana ...

  8. C++中string、char *、char[]的转换

    头段时间有人问过我这个问题,可是我一点头绪都没有,直接说不会.现在从网上找了点资料,看了看,知道点东西了. 一.string转char*. 主要有三种方法可以将str转换为char*类型,分别是:da ...

  9. request模块的使用

    安装方式 $ pip install requests 基本GET请求(headers参数 和 parmas参数) 1. 最基本的GET请求可以直接用get方法 response = requests ...

  10. Dubbo服务端结合maven打jar包

    <build>        <finalName>odao-weixin-user</finalName>        <resources>   ...