首页 技术 正文
技术 2022年11月11日
0 收藏 549 点赞 3,120 浏览 3773 个字
package com.cn.test.jihe;import java.util.Arrays;/**
*
* insert
* delete
* update
* get
*
*/
public class ArrayList {/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};Object[] elementData;private int size;protected int modCount = 0;private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;public ArrayList(int initialCapacity) throws Exception {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new Exception("illegal Capital" + initialCapacity);
}
}public ArrayList() {
this.elementData = EMPTY_ELEMENTDATA;
}public boolean add(Object obj) {
// 首先比较数组的长度
ensureCapitalInteral(size + 1);
elementData[size ++] = obj;
return false;
}public void add(int index, Object obj) throws Exception {
/**
* 首先判断要index的位置
*/
rangCheckForAdd(index);
ensureCapitalInteral(size + 1);
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = obj;
size++;
}private void rangCheckForAdd(int index) throws Exception {
if (size < index || index < 0) {
throw new Exception("数组下标越界" + index);
}
}
boolean contains(Object o ) {
return indexOf(o) >= 0;
}/**
* 返回位置上的下标
* @param index
* @return
* @throws Exception
*/
Object get(int index) throws Exception {
rangeCheck(index);
return elementData[index];
}
/**
* 列表没有元素返回true
* @return
*/
boolean isEmpty() {
return size == 0;
}/**
* 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。
* @param o
* @return
*/
int lastIndexOf(Object o) {
if (null == o) {
// 逆序循环数组
for (int i = size - 1; i >= 0; i--) {
if (null == elementData[i]) {
return i;
}
}
} else {
for (int i = size - 1; i >= 0; i--) {
if (o.equals(elementData[i])) {
return i;
}
}
}return -1;
}/**
* 移除此列表中指定位置上的元素。向左移动所有后续元素(将其索引减 1)。
* @throws Exception
*/
Object remove(int index) throws Exception {
// 下标的检查
rangeCheck(index);
Object oldValue = elementData[index];
int numMove = size - index - 1;
if (numMove > 0) { // 表示删除的不是最后一个元素
System.arraycopy(elementData, index + 1, elementData, index, numMove);
}
elementData[size--] = null;
return oldValue;
}/**
* 移除此列表中首次出现的指定元素(如果存在)。如果列表不包含此元素,则列表不做改动。
*/
boolean remove(Object object) throws Exception {
// object 是否在该数组中
int index = indexOf(object);
if(index >= 0) {
// 删除下标
int moveNum = size - index - 1;
if (moveNum > 0) {
System.arraycopy(elementData, index + 1, elementData, index, moveNum);
}
elementData[size--] = null;
return true;
}return false;}
/**
* 用指定的元素替代此列表中指定位置上的元素,
* 并且将旧元素返回
* @throws Exception
*/
Object set(int index, Object element) throws Exception {
// 1. 判断index的下标越界问题
rangeCheck(index);
Object oldValue = elementData[index];
elementData[index] = element;
return oldValue;}/**
* 按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。
由于此列表不维护对返回数组的任何引用,,因而它将是“安全的”。(换句话说,此方法必须分配一个新的数组)。因此,调用者可以自由地修改返回的数组。
* @return
*/
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}private void rangeCheck(int index) throws Exception {if (size <= index) {
throw new Exception("下标越界" + index);
}}private int indexOf(Object o) {
if(null == o) {
for (int i =0; i<size; i++) {
if (elementData[i] == null) {
return i;
}
}
} else {
for (int i = 0; i <size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}public int size() {
return size;
}
private void ensureCapitalInteral(int minCapacity) {
ensureExplicitCapacity (calculateCapacity(elementData, minCapacity));
}private void ensureExplicitCapacity(int calculateCapacity) {
modCount++;
if (calculateCapacity - elementData.length > 0) {
grow(calculateCapacity);
}}private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0) // int的数组超限
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0) {
newCapacity = hugeCapacity(minCapacity);
}
elementData = Arrays.copyOf(elementData, newCapacity);}private int hugeCapacity(int minCapacity) {
if (minCapacity < 0) {
throw new OutOfMemoryError();
}
return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}private int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY,minCapacity);
}
return minCapacity;
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297