当前位置: 移动技术网 > IT编程>开发语言>Java > ArrayList封装

ArrayList封装

2018年12月26日  | 移动技术网IT编程  | 我要评论
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;
}
}

  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网