public SimpleArrayList(){
this(DEFAULT_CAPACITY);
}
public SimpleArrayList(int size){
if (size < 0){
throw new IllegalArgumentException("默认的大小" + size);
}else{
elementData = new Object[size];
}
}
public void add(E e){
isCapacityEnough(size + 1);
elementData[size++] = e;
}
private void isCapacityEnough(int size){
if (size > DEFAULT_CAPACITY){
explicitCapacity(size);
}
if (size < 0){
throw new OutOfMemoryError();
}
}
private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8;
private void explicitCapacity(int capacity){
int newLength = elementData.length * 2;
if (newLength - capacity < 0){
newLength = capacity;
}
if (newLength > (MAX_ARRAY_LENGTH)){
newLength = (capacity > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH);
}
elementData = Arrays.copyOf(elementData, newLength);
}
if (newLength - capacity < 0){
newLength = capacity;
}
if (newLength > (MAX_ARRAY_LENGTH)){
newLength = (capacity > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH);
}
public void add(int index, E e) {
//判断是不是越界
checkRangeForAdd(index);
//判断需不需要扩容
isCapacityEnough(size + 1);
//将index的元素及以后的元素向后移一位
System.arraycopy(elementData,index,elementData,index + 1,size - index);
//将index下标的值设为e
elementData[index] = e;
size++;
}
private void checkRangeForAdd(int index){
//这里index = size是被允许的,即支持头,中间,尾部插入
if (index < 0 || index > size){
throw new IndexOutOfBoundsException("指定的index超过界限");
}
}
private void checkRange(int index) {
if (index >= size || index < 0){
throw new IndexOutOfBoundsException("指定的index超过界限");
}
}
public E get(int index){
checkRange(index);
return (E)elementData[index];
}
public int indexOf(Object o){
if (o != null) {
for (int i = 0 ; i < size ; i++){
if (elementData[i].equals(0)){
return i;
}
}
}else {
for (int i = 0 ; i < size ; i++){
if (elementData[i] == null) {
return i;
}
}
}
return -1;
}
public boolean contains(Object o){
return indexOf(o) >= 0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size() == 0;
}
public E remove(int index) {
E value = get(index);
int moveSize = size - index - 1;
if (moveSize > 0){
System.arraycopy(elementData,index + 1, elementData,index,size - index - 1);
}
elementData[--size] = null;
return value;
}
public boolean remove(Object o){
if (contains(o)){
remove(indexOf(o));
return true;
}else {
return false;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有